Android 如何在IntentService类的onHandleIntent()方法中打开对话框

Android 如何在IntentService类的onHandleIntent()方法中打开对话框,android,Android,我想在IntentService类的onHandleIntent()方法中打开一个自定义对话框,但当我编写用于在此方法中显示对话框的代码时,它会显示错误 未为MyAlarmService类型定义findViewById(int)方法 有人能给我建议如何解决这个问题吗 我使用的代码: public class MyAlarmService extends IntentService { public void onCreate() { super.onCreate(); } publi

我想在
IntentService
类的
onHandleIntent()
方法中打开一个自定义对话框,但当我编写用于在此方法中显示对话框的代码时,它会显示错误

未为MyAlarmService类型定义findViewById(int)方法

有人能给我建议如何解决这个问题吗

我使用的代码:

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();
        }
    }); 
}
}

我认为您不能在服务中膨胀视图,因为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();
    }
}

我没有试过这个,但稍加修改,这会解决你的问题。如果您还有其他问题,请不要犹豫。我不认为您可以在服务中扩大视图,因为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();
    }
}

我没有试过这个,但稍加修改,这会解决你的问题。如果您有任何其他问题,请毫不犹豫地提问。

可以这样做,只需开始一个活动,而不是想扩大对话框:

@Override
protected void onHandleIntent(Intent intent) {
    synchronized (this) 
  {
    startActivity(new Intent(this,ActivityXYZ.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
  }
}
对话框布局(这是您在上面开始的ActivityXYZ布局)设置为:

在为该活动进行布局时,只需添加文本视图和两个按钮,就像在对话框中所做的那样,它将提供从服务中膨胀的对话框效果


如需任何澄清,请让我知道。:)

只需启动一个活动即可完成,而无需考虑扩大对话框:

@Override
protected void onHandleIntent(Intent intent) {
    synchronized (this) 
  {
    startActivity(new Intent(this,ActivityXYZ.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
  }
}
对话框布局(这是您在上面开始的ActivityXYZ布局)设置为:

在为该活动进行布局时,只需添加文本视图和两个按钮,就像在对话框中所做的那样,它将提供从服务中膨胀的对话框效果


如需任何澄清,请让我知道。:)

可以从服务中显示对话框???但在我的例子中,我必须使用IntentService类,所以您知道如何在IntentService类中显示对话框。其次,如果我使用服务类,我的应用程序总是显示在正在运行的进程中,而不是缓存进程中,可以从服务中显示对话框???但在我的情况下,我必须使用IntentService类,所以你知道如何在IntentService类中显示对话框。其次,如果我使用服务类,我的应用程序总是显示在运行过程中,而不是缓存过程中。你的意思是,我可以启动一个新的活动,在该活动中,我应该打开一个对话框。但是这个活动是以dilaog的形式打开的吗你能用一个例子解释一下吗不,它会以对话框的形式弹出。只需创建一个活动,并在其布局中放置两个按钮和一个文本视图。将(如上所述)代码添加到清单中定义它的活动名称下,它肯定会工作。对于这两个按钮,你可以放在ClickListeners上,就像在我的例子中,我曾经启动过一个主屏幕,它给人一种从对话框启动应用程序的感觉。如果您仍然需要帮助或澄清,请告诉我,我将尝试用代码进行解释。请确保您将其添加到运行服务的同一项目中。希望能有帮助。好的,很好,我已经修改了我的帖子,并在你需要的时候加入了代码。你的意思是,我可以开始一个新的活动,在这个活动中我应该打开一个对话框。但是这个活动是以dilaog的形式打开的吗你能用一个例子解释一下吗不,它会以对话框的形式弹出。只需创建一个活动,并在其布局中放置两个按钮和一个文本视图。将(如上所述)代码添加到清单中定义它的活动名称下,它肯定会工作。对于这两个按钮,你可以放在ClickListeners上,就像在我的例子中,我曾经启动过一个主屏幕,它给人一种从对话框启动应用程序的感觉。如果您仍然需要帮助或澄清,请告诉我,我将尝试用代码进行解释。请确保您将其添加到运行服务的同一项目中。希望能有所帮助。好的,很好,我已经修改了我的帖子,并在你需要的时候加入了代码。