2012-07-26 98 views
0

我的代码:当通知的点击,打开活动

public class OnAlarmReceive extends BroadcastReceiver { 

     @Override 
     public void onReceive(Context context, Intent intent) 
     { 
      CharSequence title = "Hello"; 
      CharSequence message = "la!"; 
      NotificationManager notificationManager; 
      notificationManager = (NotificationManager) context.getSystemService("notification"); 
      Notification notification; 
      notification = new Notification(com.example.pack.R.drawable.ic_launcher, "test", System.currentTimeMillis()); 

      PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, null, 0); 

      notification.setLatestEventInfo(context, title, message, pendingIntent); 
      notificationManager.notify(1010, notification); 

     } 
} 

我用收到alarmmanager,我怎么能打开通知,当用户点击一个活动?

回答

1

你需要改变你的的PendingIntent声明:

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, null, 0); 

像这样的东西,将描述意图开始要启动活动(相应变化的第三个参数)

PendingIntent pendingIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), new Intent(this, YourAppClass.class), 0); 
+0

令人惊叹的是,非常感谢你@Matthieu。如果你有一些时间,也许你可以解释一下答案。谢谢。 – 2012-07-26 20:01:33

+0

最好的可能是看看谷歌的文档...你应该检查这个链接:http://developer.android.com/guide/topics/ui/notifiers/notifications.html#CreateANotification – Matthieu 2012-07-26 20:30:42

0

我有这个,我当我的应用程序从alarmmanager

public class OnAlarmReceiver extends BroadcastReceiver { 

    private static final String TAG = ComponentInfo.class.getCanonicalName(); 


@Override 
public void onReceive(Context context, Intent intent) { 
    Log.d(TAG, "Received wake up from alarm manager."); 

    long rowid = intent.getExtras().getLong(RemindersDbAdapter.KEY_ROWID); 

    WakeReminderIntentService.acquireStaticLock(context); 

    Intent i = new Intent(context, ReminderService.class); 
    i.putExtra(RemindersDbAdapter.KEY_ROWID, rowid); 
    context.startService(i); 

收到唤醒呼叫的strings.xml

<!-- Notification Bar --> 
<string name="notify_new_task_message">Wake up!</string> 
<string name="notify_new_task_title">Wake up! reminder</string> 

Wakereminderintentservice应用是另一个Java文件,我必须唤醒手机。

ReminderService.java

public class ReminderService extends WakeReminderIntentService { 

    public ReminderService() { 
    super("ReminderService"); 
     } 

@Override 
void doReminderWork(Intent intent) { 
    Log.d("ReminderService", "Doing work."); 
    Long rowId = intent.getExtras().getLong(RemindersDbAdapter.KEY_ROWID); 

    NotificationManager mgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 

    Intent notificationIntent = new Intent(this, ReminderEditActivity.class); 
    notificationIntent.putExtra(RemindersDbAdapter.KEY_ROWID, rowId); 

    PendingIntent pi = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT); 

    Notification note=new Notification(android.R.drawable.stat_sys_warning, getString(R.string.notify_new_task_message), System.currentTimeMillis()); 
    note.setLatestEventInfo(this, getString(R.string.notify_new_task_title), getString(R.string.notify_new_task_message), pi); 
    note.defaults |= Notification.DEFAULT_SOUND; 
    note.flags |= Notification.FLAG_AUTO_CANCEL; 

    // An issue could occur if user ever enters over 2,147,483,647 tasks. (Max int value). 
    // I highly doubt this will ever happen. But is good to note. 
    int id = (int)((long)rowId); 
    mgr.notify(id, note); 


    } 
} 

希望此代码的任何帮助你。