android - 我怎么知道点击通知图标打开了一个 Activity ?

标签 android

UIMain可以通过点击App图标或者Notification图标来打开,我希望定义activity打开的方式。

所以我使用 notificationIntent.putExtra("IsStartFromIcon",true) 设置了一个标签,但是 Utility.LogError(""+isStartFromIcon) 总是显示 false ,我的代码有什么问题吗?

还有其他更好的方法吗?谢谢!

public class UIMain extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_main);

        findViewById(R.id.buttonClose).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PublicParFun.DisplayIconByContent(mContext, "This is OK");
                finish();
            }
        });

        boolean isStartFromIcon=this.getIntent().getBooleanExtra("IsStartFromIcon",false);
        Utility.LogError(""+isStartFromIcon);

    }

}

PublicParFun.cs

public class PublicParFun {
  public static void DisplayIconByContent(Context myContext,String myContentText) {

        CharSequence contentTitle= myContext.getResources().getString(R.string.NotificationTitle);

        Intent notificationIntent = new Intent(myContext, ui.UIMain.class);

        notificationIntent.putExtra("IsStartFromIcon",true);

        PendingIntent contentItent = PendingIntent.getActivity(myContext, 0,notificationIntent, 0);



        Notification.Builder builder = new Notification.Builder(myContext)
                .setSmallIcon(R.drawable.icon)
                .setContentTitle(contentTitle)
                .setContentText(myContentText)
                .setContentIntent(contentItent);
        Notification notification = builder.build();
        notification.flags |= Notification.FLAG_ONGOING_EVENT; 
        notification.flags |= Notification.FLAG_NO_CLEAR; 

        NotificationManager notificationManager = (NotificationManager) myContext.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
        notificationManager.notify(myContext.getResources().getInteger(R.integer.NotificationID), notification);
    }
}

最佳答案

希望对你有帮助

  private  void generateNotification(Context context, String message ) {

        int icon = R.drawable.icon;

        long when = System.currentTimeMillis();

        NotificationManager notificationManager = (NotificationManager)

                context.getSystemService(Context.NOTIFICATION_SERVICE);

        Notification notification = new Notification(icon, message);

        String title = context.getString(R.string.app_name);

        Intent notificationIntent;      
        notificationIntent = new Intent(context, ActivityName.class);
        notificationIntent.putExtra("From", "notifyFrag");

        notificationIntent .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent =
                PendingIntent.getActivity(context, 0, notificationIntent, 0);
        notificationIntent .setLatestEventInfo(context, title, message, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notificationManager.notify(0, notification);  

在您的 Activity 类中,您可以使用以下代码检查是否通过单击通知图标打开了 Activity

在你的 oncreat 中,你可以使用

检查 Activity 是否通过通知打开
  String type = getIntent().getStringExtra("From");
if (type != null) {
    switch (type) {
        case "notifyFrag":

        // your activity called from notification 

            break;
    }
}

关于android - 我怎么知道点击通知图标打开了一个 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33561611/

相关文章:

android - Android中的 'debug.keystore'和 'release.keystore'有什么区别?

android - 如何在 Android 中集成 Azure 媒体播放器

android - java.lang.ClassCastException : android. widget.LinearLayout 无法转换为 android.widget.ScrollView

android - Activity onStop() 和销毁过程

android - 如何在 ANDROID 的 HttpURLConnection 中使用 Cookie?

android - 如何在httpurlconnection中发送带参数的get请求?

java - 在Android中使用不同组件设置表格布局中列的宽度相等

java - 如何在我的 Libgdx 游戏中制作全局高分表?

java - 在 Android 中,如何使用 VidEffects 的滤镜渲染视频

android - 使用Firebase Tasks和Rx时,不得在主应用程序线程上调用