【发布时间】:2014-10-01 11:07:28
【问题描述】:
我是通知新手。我已成功生成通知,但我想为通知编写点击事件。如果我单击通知,我想在我的项目中打开一个类。这是我的任务。
对于一般待定意图,我将打开活动,但我的任务是每 30 分钟广播运行一次。这会得到通知。
我写了代码,但是当我点击时没有打开我的应用程序。请看一下我的代码。
MainActivity
public class MainActivity extends Activity
{
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
context = this;
sendBroadcastMethod(context);
}
private void sendBroadcastMethod(Context context)
{
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (1000 * 60), pendingIntent);
Toast.makeText(this, "Alarm Started", Toast.LENGTH_SHORT).show();
Log.e("Alaram", "Alarm Started");
}
}
报警接收器
public class AlarmReceiver extends BroadcastReceiver{
private NotificationManager mNotificationManager;
Context context2;
@Override
public void onReceive(Context context, Intent intent) {
context2 = context;
callNotification();
}
private void callNotification()
{
mNotificationManager = (NotificationManager) context2.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context2);
mBuilder.setSmallIcon(R.drawable.ic_launcher);
mBuilder.setContentTitle("My notification");
mBuilder.setContentText("Hello World!");
mNotificationManager.notify(0,mBuilder.build());
}
}
它完美地显示通知,但我可以在哪里为我的通知编写点击操作。如果我在接收器类构建器中编写意图,它将不接受接收器意图形成。
【问题讨论】:
标签: android notifications broadcast