我是通知新手。我成功生成了通知,但我想爲通知編寫點擊事件。如果我點擊通知,我想在我的項目中打開一個課程。這是我的任務。如何從android中的通知中打開活動?
對於一般待定的意圖,我將打開活動,但我的任務是每30個薄荷糖廣播運行一次。那會得到通知。
我寫了代碼,但沒有打開我的應用程序,當我click.please看我的代碼一次。
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");
}
}
AlarmReceiver
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());
}
}
它完美地顯示通知,但我在哪裏可以寫點擊我的通知操作。如果我在接收器類構建器中編寫不接受接收器意圖構造的意圖。
你確定你的BroadcastReceiver正在工作嗎? – 2014-10-01 11:28:53
非常感謝,它工作正常。 – Shankar 2014-10-01 11:30:34
不客氣。不要忘記接受解決問題的答案 – 2014-10-01 11:40:51