zoukankan      html  css  js  c++  java
  • 【Android开发那点破事】消息推送BroadcastReceiver,点击通知打开两次Activity问题

    Android开发中。一般会使用BroadcastReceiver来接受Push推送消息。当APP收到推送通知时。我们须要在通知的点击事件中增加自己的逻辑。比方跳转到MainActivity。


    比方以下的代码(注意红色部分):

    public void onReceive(Context context, Intent intent) {
            Bundle bundle = intent.getExtras();
            if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
                String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
                Log.d(TAG, "[JPushReceiver] 接收Registration Id : " + regId);
                //send the Registration Id to your server...
                            
            } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            	Log.d(TAG, "[JPushReceiver] 接收到推送下来的自己定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
            
            } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
                Log.d(TAG, "[JPushReceiver] 接收到推送下来的通知");
                int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
                Log.d(TAG, "[JPushReceiver] 接收到推送下来的通知的ID: " + notifactionId);
            	
            } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
                Log.d(TAG, "[JPushReceiver] 用户点击打开了通知");
                JPushInterface.reportNotificationOpened(context, bundle.getString(JPushInterface.EXTRA_MSG_ID));
                
            	// 打开自己定义的Activity
            	Intent i = new Intent(context, MainTabActivity.class);
            	i.putExtras(bundle);
            	i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            	context.startActivity(i);
            	
            } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
                Log.d(TAG, "[MyReceiver] 用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));
                //在这里依据 JPushInterface.EXTRA_EXTRA 的内容处理代码。比方打开新的Activity, 打开一个网页等..
            	
            } else {
            	Log.d(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction());
            }
    }

    使用Activity以外的content来startActivity,必须指定为Intent.FLAG_ACTIVITY_NEW_TASK。
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);


    我们能够尝试下使用Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT,会抛出以下异常:

    ERROR/AndroidRuntime(7557): java.lang.RuntimeException: Unable to start receiver com.wcc.Wakeup: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

    很多其它关于Intent的flag。能够參考:http://developer.android.com/reference/android/content/Intent.html



    所以仅仅能使用FLAG_ACTIVITY_NEW_TASK。使用这个还有个问题,就是会新打开一个MainActivity。怎样解决问题?
    事实上非常easy在AndroidManifest.xml中将MainActivity定义为:
     android:launchMode="singleTask" 就可以:
    <activity android:name="com.withiter.quhao.activity.MainTabActivity" android:launchMode="singleTask" android:label="@string/app_name" android:screenOrientation="portrait">

    这样每次打开推送。就不会出现2个activity的情况了。

    很多其它关于activity的launchMode能够參考:http://developer.android.com/guide/topics/manifest/activity-element.html#lmode


    好了今天这个破事就到这里,事实上android开发就这么点破事。

    关于其它破事。见专栏:

    很多其它Android开发的破事。请看专栏:《Android开发那点破事》

  • 相关阅读:
    ubuntu studio
    BeanUtils包的学习
    java的GUI编程
    Mybatis之旅第三篇-SqlMapConfig.xml全局配置文件解析
    Mybatis之旅第二篇-Mapper动态代理方式
    Mybatis之旅第一篇-初识Mybatis
    Spring之旅第六篇-事务管理
    Spring之旅第五篇-AOP详解
    Spring之旅第三篇-Spring配置详解
    Spring之旅第二篇-Spring IOC概念及原理分析
  • 原文地址:https://www.cnblogs.com/mqxnongmin/p/10869230.html
Copyright © 2011-2022 走看看