我有两个活动,A(登录)和B(DashBoard),我创建特定时间的通知,如果用户已经在活动B,那么点击通知什么都不应该发生,如果应用程序关闭或不打开,点击通知需要打开活动A.如何实现这一目标.TIA
我在braodcastreceiver中尝试了以下内容
@Override
public void onReceive(Context context, Intent intent) {
Log.e("NotificationReceiver","NotificationReceiver");
if(ActivityB.instance!=null){
}else{
Intent i = new Intent();
i.setClassName("com.test","com.test.ActivityA");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
但是当我的应用程序没有打开或关闭时,其他的都没有执行。
答案 0 :(得分:0)
使用activityLifeCycleCallbacks
跟踪应用程序类中的活动public class MyApplication extends Application implements my.package.ActivityLifecycleCallbacks{
@Override
public void onCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(this);
}
@Override
public void onActivityStopped(Activity activity) {
Log.i("Tracking Activity Stopped", activity.getLocalClassName());
}
@Override
public void onActivityStarted(Activity activity) {
Log.i("Tracking Activity Started", activity.getLocalClassName());
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
Log.i("Tracking Activity SaveInstanceState", activity.getLocalClassName());
}
@Override
public void onActivityResumed(Activity activity) {
Log.i("Tracking Activity Resumed", activity.getLocalClassName());
}
@Override
public void onActivityPaused(Activity activity) {
Log.i("Tracking Activity Paused", activity.getLocalClassName());
}
@Override
public void onActivityDestroyed(Activity activity) {
Log.i("Tracking Activity Destroyed", activity.getLocalClassName());
}
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
Log.i("Tracking Activity Created", activity.getLocalClassName());
}
}
接下来收到广播中的通知onClick,检查您的应用程序类是否打开了activityB并做出决定