我尝试使用此代码创建通知:
private void setNotificationAlarm(Context context)
{
Intent intent = new Intent(getApplicationContext() , MyNotification.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 , pendingIntent);
Log.d("ME", "Alarm started");
}
public class MyNotification extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Log.d("ME", "Notification started");
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification")
.setContentText("Hello World!");
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
}
这里是我的 Mainfest 声明:
<receiver
android:name=".MyNotification"
android:enabled="true"
android:exported="false" >
</receiver>
我现在的问题是,警报已生成但未显示通知。BroadcastReceiver 在 mainfest 文件中声明,没有编译器或运行时错误。
我的第二个问题是,setLatestEventInfo
Contructornew Notification
已被弃用。我可以用什么来代替它?