2014-09-10 30 views
0

我成功地创建了启动一个服务,它通过转这样表示恢复应用

public int onStartCommand(Intent intent, int flags, int startId){ 

String notificationText = "working" 
     myNotification = new NotificationCompat.Builder(getApplicationContext()) 
       .setContentTitle("test") 
       .setContentText(notificationText) 
       .setTicker("Notification!") 
       .setWhen(System.currentTimeMillis()) 
       .setDefaults(Notification.DEFAULT_SOUND) 
       .setAutoCancel(true) 
       .setSmallIcon(R.drawable.ic_launcher) 
       .build(); 

//FOLLOWING TECHNIQUE IS DEPRECATED 
/* PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0); 
    myNotification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); */ 

     myNotification.flags |= Notification.FLAG_NO_CLEAR; 
     myNotification.flags = Notification.FLAG_ONGOING_EVENT; 
     notificationManager.notify(1, myNotification); 

     return START_STICKY; 
} 

我知道有与此相关的其他问题,但它们都使用的通知活动setLatestEventInfo它被废弃

这是我从另一个活动调用:

protected void beginBackgroundSer(){ 

     intentService = new Intent(this, Service.class); 
     intentService.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
     this.startService(intentService); 

    } 

所以我想要的是简历每当我点击通过非弃用方法的活动。非常感谢您的帮助。

回答

0

您可以通过以下步骤创建一个通知。

  1. 创建通知ID以供将来参考,以通过通知管理器更新通知。 // Notification ID to allow for future updates private static final int MY_NOTIFICATION_ID = 1;
  2. 为通知创建文本元素。

    // Notification Text Elements 
    private final CharSequence tickerText = "This is a Really, Really, Super Long Notification Message!"; 
    private final CharSequence contentTitle = "Notification"; 
    private final CharSequence contentText = "You've Been Notified!"; 
    
  3. 定义通知动作要素,即当用户点击在通知抽屉什么动作发生的观点?我们使用意图来处理这个问题。

    // Notification Action Elements 
    private Intent mNotificationIntent; 
    private PendingIntent mContentIntent; 
    
  4. 您还可以定义此通知的声音和振动。

    // Notification Sound and Vibration on Arrival 
    private Uri soundURI = Uri 
         .parse("android.resource://com.examples.Notification.StatusBarWithCustomView/" 
           + R.raw.alarm_rooster); 
    private long[] mVibratePattern = { 0, 200, 200, 300 }; 
    
  5. 使用Notification.Builder类创建通知。在您的onCreate()

    mNotificationIntent = new Intent(getApplicationContext(), 
         YourActivity.class); 
    mContentIntent = PendingIntent.getActivity(getApplicationContext(), 0, 
         mNotificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK); 
    
    
          // Build the Notification 
    
          Notification.Builder notificationBuilder = new Notification.Builder(
            getApplicationContext()) 
           .setTicker(tickerText) 
           .setSmallIcon(android.R.drawable.stat_sys_warning) 
           .setAutoCancel(true) 
           .setContentIntent(mContentIntent) 
           .setSound(soundURI) 
           .setVibrate(mVibratePattern) 
           .setContent(mContentView); 
    
          // Pass the Notification to the NotificationManager: 
          NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
          mNotificationManager.notify(MY_NOTIFICATION_ID, 
            notificationBuilder.build()); 
    
0

documentations

公共NotificationCompat.Builder setContentIntent(的PendingIntent意图) - 提供一个的PendingIntent点击通知时发送。如果您没有提供意图,现在可以通过调用RemoteViews.setOnClickPendingIntent(int,PendingIntent)将PendingIntents添加到单击的视图中以启动它们。请务必阅读Notification.contentIntent以了解如何正确使用它。

所以,你可以使用setContentIntent(PendingIntent intent)方法类似

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0); 
myNotification = new NotificationCompat.Builder(getApplicationContext()) 
      .setContentTitle("test") 
      .setContentText(notificationText) 
      .setTicker("Notification!") 
      .setWhen(System.currentTimeMillis()) 
      .setDefaults(Notification.DEFAULT_SOUND) 
      .setAutoCancel(true) 
      .setSmallIcon(R.drawable.ic_launcher) 
      .setContentIntent(contentIntent) //added this new line 
      .build(); 

notificationManager.notify(1, myNotification); 
+0

当我点击它会提醒“很抱歉,但应用程序已中断”的通知执行此操作。任何想法为什么? – 2014-09-10 22:40:51

+0

我看到你接受了一个答案,但你是否得到了你在做错什么?这很重要。你需要知道为什么它说“我们很抱歉,但应用程序已被中断”。 – 2014-09-10 22:54:52