1

onHandleIntent()クラスのメソッドでカスタムダイアログを開きたいのです IntentServiceが、このメソッドでダイアログを表示するコードを書くとエラーが表示されます

The method findViewById(int) is undefined for the type MyAlarmService

誰かがこの問題を解決する方法を教えてもらえますか?

私が使用したコード:

public class MyAlarmService extends IntentService { 
public void onCreate() {
    super.onCreate();
}

public MyAlarmService() {
    super("MyAlarmService");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, startId, startId);
    Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show();
    return START_STICKY;
}
            
@SuppressWarnings("static-access")
@Override
protected void onHandleIntent(Intent intent) {
    final Dialog alarmDialog = new Dialog(MyAlarmService.this);
    alarmDialog .requestWindowFeature(alarmDialog.getWindow().FEATURE_NO_TITLE);
    alarmDialog .getWindow().setBackgroundDrawable(new ColorDrawable(0));

    Context mContext = getApplicationContext();
    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.clear_all_warning_dialog, (ViewGroup) findViewById(R.id.layout_warning_dialog));
    
    TextView dialogTitile = (TextView) layout.findViewById(R.id.dialog_title_text);
    
    TextView dialogDesc = (TextView) layout.findViewById(R.id.dialog_desc_text);
    
    Button buttonYes = (Button) layout.findViewById(R.id.button_yes);               
    
    Button buttonNo = (Button) layout.findViewById(R.id.button_no);             

    alarmDialog.setContentView(layout);
    alarmDialog.show();
    
    buttonYes.setOnClickListener(new OnClickListener() {                    
        @Override
        public void onClick(View v) {
            alarmDialog.dismiss();
        }
    }); 
    buttonNo.setOnClickListener(new OnClickListener() {                 
        @Override
        public void onClick(View v) {
            alarmDialog.dismiss();
        }
    }); 
}
}
4

2 に答える 2

4

ダイアログを膨らませることを考える代わりに、アクティビティを開始するだけで実行できます。

@Override
protected void onHandleIntent(Intent intent) {
    synchronized (this) 
  {
    startActivity(new Intent(this,ActivityXYZ.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
  }
}

ダイアログレイアウト(上記で開始したActivityXYZのレイアウト)を次のようにします。

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000"
android:orientation="vertical" >

<TextView
    android:id="@+id/TextView01"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_margin="10dp"
    android:gravity="center_horizontal"
    android:text="Alert Dialog Message"
    android:textColor="#fff"
    android:textSize="16dp" >
</TextView>
<LinearLayout  android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_marginBottom="5dp">

<Button
    android:id="@+id/ButtonOk"
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:text="Ok"
    android:textColor="#000"
    android:textSize="18sp"
    android:textStyle="bold" >
</Button>

<Button
    android:id="@+id/ButtonCancel"
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:text="Cancel"
    android:textColor="#000"
    android:textSize="18sp"
    android:textStyle="bold"
   >
</Button>
</LinearLayout>
</LinearLayout>

マニフェストファイルでは、これをそのアクティビティの構成に含めます。

android:theme="@android:style/Theme.Dialog"

そのアクティビティのレイアウトを作成している間、ダイアログで行われたようにテキストビューと2つのボタンを追加するだけで、サービスから膨らんだダイアログの効果が得られます。

ご不明な点がございましたら、お気軽にお問い合わせください。:)))))

于 2012-07-02T12:26:53.260 に答える
0

IntentService は UI スレッドで実行されないため、サービスでビューを膨らませることはできないと思います。別の解決策は、サービスをアクティビティにバインドし、ダイアログを表示してサービスに登録するリスナーを作成することです。 onBind() メソッドで。

次の方法でサービスを拡張できます。

public class MyAlarmService extends IntentService {

    private MyListener mListener;

    // Your implementation here

    public void registerListener(MyListener listener){
        mListener = listener;
    }

    @Override
    protected void onHandleIntent(Intent intent) {
                listener.postToUIThread();

        // Other things you might want to do
    }

    @Override
    public IBinder onBind(Intent intent) {
        return new MyAlarmBinder(this);
    }

    @Override
    public boolean onUnbind(Intent intent) {
        return super.onUnbind(intent);
    }
}

次に、バインダーとリスナーを次のように記述します。

public class MyListener {

    private Handler mHandler;
    private Runnable mRunnable;

    public MyListener(Handler handler, Runnable runnable){
        this.mHandler = handler;
        this.mRunnable = runnable;
    }

    public void postToUIThread(){
        mHandler.post(mRunnable);
    }
}

public class MyAlarmBinder extends Binder {
    private WeakReference<MyAlarmService> mService;

    public MyAlarmBinder(MyAlarmService service){
        mService = new WeakReference<MyAlarmService>(service);
    }

    public MyAlarmService getService(){
        return mService.get();
    }
}

リスナーでは、UI スレッドで作成された Handler を使用して、ダイアログ作成コードを UI スレッドにポストしていることに注意してください。

そして、あなたの主な活動では、それをまとめることができます:

private MyAlarmService mService;

private Handler mHandler = new Handler;
private Runnable mRunnable = new Runnable(){

    @Override
    public void run(){
        // Show dialog here
    }
}

private ServiceConnection mConnection = new ServiceConnection() {

    public void onServiceConnected(ComponentName name, IBinder service) {
        mService = ((MyAlarmBinder)service).getService();
        mService.registerListener(new MyListener(mHandler, mRunnable));
    }

    public void onServiceDisconnected(ComponentName name) {
        mService = null;
    }
};

@Override
protected void onResume() {
    super.onResume();
    bindService(new Intent(this, MyAlarmService.class), mConnection,
        Context.BIND_AUTO_CREATE);
}

@Override
protected void onPause() {
    super.onPause();
    if(mConnection != null){
        unbindService(mConnection);
    }
}

私はこれを試しませんでしたが、わずかな変更で問題が解決するはずです。他にご不明な点がございましたら、お気軽にお尋ねください。

于 2012-07-02T11:57:27.303 に答える