有目的地将活动放在首位



>Hy,

我正在制作一个简单的秒表。当我单击"开始"按钮时,状态栏中会显示一条通知。当我单击通知时,我的秒表停止并重置为 0。以下是通知的意图:

            Intent msgIntent = new Intent(this, myActivity.class);
            msgIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            PendingIntent intent = PendingIntent.getActivity(myActivity.this,
                    0, msgIntent,
                    Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

如果添加标志没有按预期工作,满足需求的最简单方法是在清单文件中定义特定活动的启动模式。例如:

<activity
        android:name=".myActivity"
        android:label="@string/app_name"
        android:launchMode="singleTop" >

尝试以下虚拟操作:

        Intent msgIntent = new Intent(this, myActivity.class);
        msgIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        msgIntent.putExtas(STOP, true);
        PendingIntent intent = PendingIntent.getActivity(myActivity.this,
                0, msgIntent,
                PendingIntent.FLAG_CANCEL_CURRENT);
       //In your activity:
       onCreate() {
           if( getIntent().getBoolean(STOP) ) 
               then stop
       }
       onNewIntent(Intent _intent) {
          if( _intent.getBoolean(STOP) ) 
               then stop
       }

我使用这种方法,问题解决了,希望这也可以帮助您;

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setClass(this, Main.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

最新更新