来自NotificationBar的Android startActivity

时间:2014-09-22 12:31:27

标签: android android-intent android-activity android-notification-bar

下面的代码是我简单的NotificationBar类,我可以在所有项目中使用它。我想将Activity设为Intent StartActivity。{如何更新我的类以获得此功能?

public class TsmsNotification /* extends Activity*/ {

    private Context context;
    public TsmsNotification( Context context ) {
        this.context = context;
    }

    public void SetNotifyText( String title , String text){
        Notification notification = new Notification(
                R.drawable.tsms_icon,
                title, //Text
                System.currentTimeMillis() //When to display - i.e. now
        );
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(Intent.ACTION_VIEW);
        PendingIntent pi = PendingIntent.getActivity(this.context, 0, intent, 0);

        //Add to the Notification
        notification.setLatestEventInfo(
                this.context,
                title, //Title of detail view
                text , pi
        );

        //Display the Notification
        NotificationManager nm = (NotificationManager)this.context.getSystemService(Context.NOTIFICATION_SERVICE);
        nm.notify(0, notification);

        /*intent.setClass(TsmsNotification.this, ReceivedSMSFragment.class);*/
    }
}

我可以将此课程用作:

new TsmsNotification(context)
                .SetNotifyText(
                        context.getString(R.string.notify_text),
                        context.getString(R.string.total_inbox_sms)
                );

1 个答案:

答案 0 :(得分:0)

通常您可以通过以下通知启动活动:

       Intent startActivityIntent = new Intent(context, YourActivity.class);
       PendingIntent pi= PendingIntent.getActivity(context,
            0, startActivityIntent ,
            PendingIntent.FLAG_CANCEL_CURRENT);

       notification.setLatestEventInfo(
            this.context,
            title, //Title of detail view
            text , pi
    );

    //Display the Notification
    NotificationManager nm = (NotificationManager)this.context.getSystemService(Context.NOTIFICATION_SERVICE);
    nm.notify(0, notification);

点击通知,活动应该开始。