finish background activity from broadcast receiver

狂风中的少年 提交于 2019-12-25 07:40:04

问题


I launch a new activity "ActivityB" when keypad is locked.(ActivityA has been backgrounded before the keypad is locked). ActivityB times out after 30 secs and supposed to close itself, so I called finish after 30 secs, though is not visible, after I unlock I see 2 seperate apps/activities in background. So I used Intent.ACTION_USER_PRESENT broadcastreceiver to finish activityB, still it doesnt work.

Manifest.xml

  <receiver
            android:name="com.example.reciever.UnlockReceiver">
            <intent-filter>
                <action android:name="android.intent.action.USER_PRESENT" />
            </intent-filter>
        </receiver>

UnlockReceiver: public class UnlockReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context arg0, Intent intent) {
        if (ActivityB.b != null) {
            ActivityB.b .finish();
        }
}

}

ActivityB:

private Activity b;
onCreate() {
b= this;
}

ActivityB is started as we receive push:

Intent pushIntent = new Intent(context.getApplicationContext(), ActivityB.class);
                pushIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

I see the onReceive called fine when I unlock the device, but it doesn't finsih ActivityB in the background. Hence I see 2 of the same apps in background


回答1:


you may have an intent in activity a which is creating the activity b;




回答2:


The issue was fixed after I set the below property in manifest file android:launchMode="singleTop"



来源:https://stackoverflow.com/questions/39882530/finish-background-activity-from-broadcast-receiver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!