收到通知时自动打开Android活动 [英] Open Android activity automatically on receiving notification

查看:1399
本文介绍了收到通知时自动打开Android活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在收到通知后启动应用程序。当应用程序被终止并收到通知时(即条件内部的代码),以下代码工作正常。但是当应用程序在前台或后台运行时,会创建多个活动实例(即其他条件中的代码段)。这不是在接收通知时必须启动的MainActivity,而是包含广播Receiver的其他一些活动。
我在GCMintentService类的onMessage中添加了以下几行。

  if(currentPackage.equalsIgnoreCase(context.getPackageName ()
.toString())){
broadcastMessage(context,message);
} else {
Intent mIntent = new Intent(context,MainActivity.class);
mIntent.setAction(Intent.ACTION_MAIN);
mIntent.addCategory(Intent.CATEGORY_LAUNCHER);
mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mIntent);

$ / code>

在activity中,在BroadcastReceiver的onReceive方法下,我再次启动活动。

  private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver(){
@Override $ b $ public void onReceive(Context上下文,意图意图){
startActivity(getIntent()。setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT));
}
};


解决方案

我的应用程序中也有这个要求。如果我们调用

  Intent mIntent = new Intent(context,MainActivity.class); 
mIntent.setAction(Intent.ACTION_MAIN);
mIntent.addCategory(Intent.CATEGORY_LAUNCHER);
mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mIntent);
broadcastMessage(context,message);

在主要活动中,在广播接收器中使用以下广播消息。

  WakeLock wakeLock = null; 
KeyguardManager kgMgr =(KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
boolean locked = kgMgr.inKeyguardRestrictedInputMode();
PowerManager pm =(PowerManager)上下文
.getSystemService(Context.POWER_SERVICE);
if(!pm.isScreenOn()){
wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP,MyWakeLock);
wakeLock.acquire();
}
if(locked){
Window mWindow = getWindow();
mWindow.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
mWindow.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
}

我个人觉得这不是最好的答案,也是最好的当接收到通知时会直接打开应用程序的想法,因为会有许多功能,如onCreate onResume,会自动触发,如果用户通过直接打开另一个应用程序进行真正重要的工作,会损坏用户的工作,同时我们还需要将许多标志或使用任何其他方法来管理应用程序的流程,当用户打开应用程序时,应用程序来自后台,应用程序开放的应有通知以及所有这些情况。避免它,因为它会损害整个用户体验。

I have to launch the app on receiving notification. The following piece of code works fine when the app is killed and notification is received (i.e the code inside if condition). But when the app is running in foreground or background, multiple instances of the activity gets created(i.e snippet in else condition). It's not the MainActivity that has to be launched on receiving the notification, instead it's some other activity containing the broadcast Receiver. I have added the following lines in the onMessage of GCMintentService class.

if (currentPackage.equalsIgnoreCase(context.getPackageName()
                .toString())) {
            broadcastMessage(context, message);
        } else {
            Intent mIntent = new Intent(context, MainActivity.class);
            mIntent.setAction(Intent.ACTION_MAIN);
            mIntent.addCategory(Intent.CATEGORY_LAUNCHER);
            mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(mIntent);
        }

In the activity, under onReceive method of BroadcastReceiver, i am starting the activity again.

private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        startActivity(getIntent().setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT));
    }
};

解决方案

I also had this requirement in one of my application. We can achieve it if we call

Intent mIntent = new Intent(context, MainActivity.class);
                mIntent.setAction(Intent.ACTION_MAIN);
                mIntent.addCategory(Intent.CATEGORY_LAUNCHER);
                mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(mIntent);
broadcastMessage(context, message);

In the main activity use the following in the broadcast receiver that would receive the broadcasted message above.

WakeLock wakeLock = null;
        KeyguardManager kgMgr = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
        boolean locked = kgMgr.inKeyguardRestrictedInputMode();
        PowerManager pm = (PowerManager) context
                .getSystemService(Context.POWER_SERVICE);
        if (!pm.isScreenOn()) {
            wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
                    | PowerManager.ACQUIRE_CAUSES_WAKEUP, "MyWakeLock");
            wakeLock.acquire();
        }
        if (locked) {
            Window mWindow = getWindow();
            mWindow.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
            mWindow.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        } 

Personally I feel that this is not the best of the answers and also best of the ideas to open the app directly when received a notification as there will be many functions like onCreate onResume, will be triggered automatically, spoil the users work if they are in a really important work by opening another app directly, also we need to put a lot of flags or use any other method to manage the flow of the application, when user open the app, app comes from background, app opened due notification and all such cases. Avoid it as it spoils the whole user experience.

这篇关于收到通知时自动打开Android活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆