如何从服务关闭活动?

2023-09-12 10:02:17 作者:一身傲气王

从基于一定的价值的服务

我发起活动从服务器得到了与该活动将显示在一段时间,越来越接近指令从我需要关闭该活动服务器后,所以我用下面的方法但它不工作:  在服务类:

if(((ActivityManager)this.getSystemService(ACTIVITY_SERVICE)).getRunningTasks(1).get(0).topActivity.getPackageName().equals("com")) { if(((ActivityManager)this.getSystemService(ACTIVITY_SERVICE)).getRunningTasks(1).get(0).topActivity.getClassName().equals("com.CustomDialogActivity")){ 意图dialogIntent =新的意图(getBaseContext(),CustomDialogActivity.class);              dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);              dialogIntent.putExtra(说明,);              dialogIntent.putExtra(cancelEnabled,假);              dialogIntent.putExtra(关闭,真正的);             。getApplication()startActivity(dialogIntent); }     }

和onCreate方法里面活动:

 捆绑包= getIntent()getExtras()。
布尔isClosed返= bundle.getBoolean(亲密);
    如果(isClosed返){
        完();
 }
 

我调试了一下,发现控制达到了onCreate方法,如果(在isClosed)条件并执行完成()方法还,但它不打烊活动。

所以无法能够分析自己做错了什么,我做什么。

手机QQ如何彻底关闭动态中的服务提示

请帮我解决这个问题。

问候, 皮克斯。

解决方案

写广播接收器在你的CustomDialogActivity像下面。

 私人最终的BroadcastReceiver ABCD =新的BroadcastReceiver(){
             @覆盖
             公共无效的onReceive(上下文的背景下,意图意图){
                   完();
             }
     };
 

然后在同一文件中像下面注册:

 的onCreate(){

    registerReceiver(ABCD,新的IntentFilter(XYZ));
}
 

注销它的onDestroy。

 的onDestroy(){

   //注销
}
 

现在,每当你想关闭该活动只是调用如下所示。

  sendBroadcast(新的意向书(XYZ));
 

希望这有助于。

I am launching activity from the service based on some value got from the server and the activity will be displayed for the some time and after getting close instruction from the server i need to close that activity,so for that i used following approach but it's not working: on Service class:

if(((ActivityManager)this.getSystemService(ACTIVITY_SERVICE)).getRunningTasks(1).get(0).topActivity.getPackageName().equals("com")) {

if(((ActivityManager)this.getSystemService(ACTIVITY_SERVICE)).getRunningTasks(1).get(0).topActivity.getClassName().equals("com.CustomDialogActivity")){
Intent dialogIntent = new Intent(getBaseContext(), CustomDialogActivity.class);
             dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);               
             dialogIntent.putExtra("description", "");
             dialogIntent.putExtra("cancelEnabled", false);
             dialogIntent.putExtra("close", true);
            getApplication().startActivity(dialogIntent);


}
    }

and on activity inside onCreate method:

Bundle bundle = getIntent().getExtras();
boolean isClosed = bundle.getBoolean("close");
    if(isClosed){        
        finish();
 }

I debugged it and found that control reaches to the onCreate method if(isClosed) condition and executed finish() method also but its not closing the activity.

so Couldn't be able to analyze what wrong I am doing.

Please help me solve this issue.

Regards, Piks.

解决方案

Write a broadcast receiver in your CustomDialogActivity like following.

private final BroadcastReceiver abcd = new BroadcastReceiver() {
             @Override
             public void onReceive(Context context, Intent intent) {
                   finish();                                   
             }
     };

Then register it in the same file like the following:

onCreate(){

    registerReceiver(abcd, new IntentFilter("xyz"));
}

Unregister it in onDestroy.

onDestroy(){

   //unRegister
}

Now,Whenever you want to close that Activity just call like the following.

sendBroadcast(new Intent("xyz"));

Hope this help.

相关推荐