1
我想在Android模擬器上添加一個簡單的通知。我在android開發者網站上取得了Notifications的代碼。我已經使用代碼在仿真器上運行,但沒有通知。我是否缺少任何許可或某種設置,任何人都可以請幫助我。模擬器上的通知不起作用
public class SampleNotificationActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample_notification);
}
@Override
protected void onResume() {
super.onResume();
sendNotification();
}
private void sendNotification() {
NotificationCompat.Builder notificationBuilder
= new NotificationCompat.Builder(SampleNotificationActivity.this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Notification cool")
.setContentText("My Very first Notification");
Intent resultIntent = new Intent(this, NotificationActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(SampleNotificationActivity.this);
stackBuilder.addParentStack(NotificationActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContentIntent(resultPendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(100, notificationBuilder.build());
}
}
可以添加完整的代碼,看看滿級? –
@BrunoFerreira,我添加了活動類 – MaheshGupta
如果將sendnotification添加到oncreate,會發生什麼情況。 –