我有一个名为MainActivity.java的活动,其中包含“自定义通知生成器”。这是代码:

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
Button bcustomnotify = (Button) findViewById(R.id.customnotification);
bcustomnotify.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
            CustomNotification();
        }
    });
}

public void CustomNotification() {
    // Using RemoteViews to bind custom layouts into Notification
    RemoteViews remoteViews = new RemoteViews(getPackageName(),
            R.layout.customnotification);

    // Set Notification Title
    String strtitle = getString(R.string.customnotificationtitle);
    // Set Notification Text
    String strtext = getString(R.string.customnotificationtext);

    // Open NotificationView Class on Notification Click
    Intent intent = new Intent(this, NotificationView.class);
    // Send data to NotificationView Class
    intent.putExtra("title", strtitle);
    intent.putExtra("text", strtext);
    // Open NotificationView.java Activity
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            // Set Icon
            .setSmallIcon(R.mipmap.ic_launcher)
            // Set Ticker Message
            .setTicker(getString(R.string.customnotificationticker))
            // Dismiss Notification
            .setOngoing(true)
            // Set PendingIntent into Notification
            .setContentIntent(pIntent)
            // Set RemoteViews into Notification
            .setContent(remoteViews);

    // Locate and set the Image into customnotificationtext.xml ImageViews
    remoteViews.setImageViewResource(R.id.imagenotileft, R.mipmap.ic_launcher);
    remoteViews.setImageViewResource(R.id.imagenotiright, R.mipmap.ic_launcher);

    // Locate and set the Text into customnotificationtext.xml TextViews
    remoteViews.setTextViewText(R.id.title, getString(R.string.customnotificationtitle));
    remoteViews.setTextViewText(R.id.text, getString(R.string.customnotificationtext));

    // Create Notification Manager
    NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    // Build Notification with Notification Manager
    notificationmanager.notify(0, builder.build());

}
}


而且我在另一个活动中有一个名为anotherActivity.java的类,它像这样:

public class anotherActivity extends Activity {
// some code
}


现在的问题是

单击MainActivity的通知后是否可以运行另一个活动的类,而不必实际将另一个活动作为未决的意图打开?

最佳答案

单击MainActivity的通知后是否可以运行另一个活动的类,而不必实际将另一个活动作为未决的意图打开?


没有。

如果要点击Notification来执行没有可见UI的操作,请不要在getActivity()上使用PendingIntent。使用getService()getBroadcast()并路由到适当的组件。

如果您要点击Notification以显示一个活动(显然命名为NotificationView)并且还执行AnotherActivity的某项操作,则应将AnotherActivity合并到NotificationView中,或者合并为通用代码重构为另一个类或其他东西。您的PendingIntent可以做一件事:开始一项活动,开始一项服务或发送广播。

07-24 09:49
查看更多