このコードで通知を作成しようとしました:
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 ファイルで宣言されており、コンパイラ エラーや実行時エラーはありません。
私の 2 番目の問題はsetLatestEventInfo
、new Notification
Contructor が非推奨であることです。代わりに何を使用できますか?