开始秒表的点击事件开始通知

时间:2013-11-27 05:52:56

标签: android timer notifications

我想在点击通知时点击开始按钮计时器时启动计时器和通知。当点击通知时,应该在另一个活动中注意计时器时间这是我的代码到目前为止我做的。登记/>  btn.setOnClickListener(new View.OnClickListener() {

     public void onClick(View v)
  {
            Intent intent = new Intent(context, MainActivity.class);
             PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
            Notification noti = new Notification.Builder(context)
            .setContentTitle("New notification")
            .setContentText("Subject").setSmallIcon(R.drawable.ic_launcher)
            .setContentIntent(pIntent).build();
            NotificationManager notificationManager = 
            (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
            // hide the notification after its selected
            noti.flags |= Notification.FLAG_AUTO_CANCEL;
            notificationManager.notify(0, noti);

        }
    });

2 个答案:

答案 0 :(得分:0)

试试这个:

private void generateNotification(Context context, String message) {

int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
String appname = context.getResources().getString(R.string.app_name);
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
Notification notification;
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(context, myactivity.class), 0);

// To support 2.3 os, we use "Notification" class and 3.0+ os will use
// "NotificationCompat.Builder" class.
if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
notification = new Notification(icon, message, 0);
notification.setLatestEventInfo(context, appname, message,
contentIntent);
notification.flags = Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);

} else {
NotificationCompat.Builder builder = new NotificationCompat.Builder(
context);
notification = builder.setContentIntent(contentIntent)
.setSmallIcon(icon).setTicker(appname).setWhen(0)
.setAutoCancel(true).setContentTitle(appname)
.setContentText(message).build();

notificationManager.notify(0 , notification);
}
}

希望这有帮助。

答案 1 :(得分:0)

因此,当您点击秒表的开始按钮时,请致电通知管理器并显示通知。

btn.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                Intent intent = new Intent(context, MainActivity.class);
                PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
                Notification noti = new Notification.Builder(context)
                .setContentTitle("New notification")
                .setContentText("Subject").setSmallIcon(R.drawable.ic_launcher)
                .setContentIntent(pIntent).build();
                NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                // hide the notification after its selected
                noti.flags |= Notification.FLAG_AUTO_CANCEL;
                notificationManager.notify(0, noti);

            }
        });