0

起動完了後にインテント、alarmamager、次に通知バーを開始するアプリを作成しました。再起動後、通知クラスが実行されますが、一定の時間にバーに通知が表示されませんでした。

別の質問: AlarmReceiver クラスを呼び出さずに Notify クラスから通知バーを開始できますか?

自動スタート

public class AutoStart extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
        Intent i = new Intent(context, Notify.class);  
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);  
}

通知する

public class Notify extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

Calendar cal1 = Calendar.getInstance();
cal1.set(Calendar.HOUR_OF_DAY, 05);
cal1.set(Calendar.MINUTE, 45);
cal1.set(Calendar.SECOND, 00);

Calendar cal2 = Calendar.getInstance();
cal2.set(Calendar.HOUR_OF_DAY, 17);
cal2.set(Calendar.MINUTE, 30);
cal2.set(Calendar.SECOND, 00);

Calendar now = Calendar.getInstance();
if(now.after(cal1))
    cal1.add(Calendar.HOUR_OF_DAY, 24);
if(now.after(cal2))
    cal2.add(Calendar.HOUR_OF_DAY, 24);

Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent morningAlarm = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
PendingIntent eveningAlarm = PendingIntent.getBroadcast(getApplicationContext(), 1, intent, 0);

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal1.getTimeInMillis(), DateUtils.DAY_IN_MILLIS, morningAlarm);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal2.getTimeInMillis(), DateUtils.DAY_IN_MILLIS, eveningAlarm);

アラーム受信者:

public class AlarmReceiver extends BroadcastReceiver {

    final String not1[] = new String[37];
    int not1end = 36;
    int x;

    public void onReceive(Context context, Intent intent) {

not1[0]="one";
        not1[1]="two";
        not1[2]="three";


     String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);

        int icon = R.drawable.ic_launcher;
        CharSequence tickerText = "Votre vidange approche";
        long when = System.currentTimeMillis();

        Notification notification = new Notification(icon, tickerText, when);


        CharSequence contentTitle = "Notification";
        CharSequence contentText = "Vérifier votre kilométrage";
        Intent notificationIntent = new Intent("org.gortcloud.perledisaggezza");
//        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

        final int HELLO_ID = 1;

        mNotificationManager.notify(HELLO_ID, notification);
     }     

マニフェスト

<receiver 
    android:name=".Notify"
    android:exported="false" >
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

<receiver android:name="AlarmReceiver" >
            <intent-filter>
                <action android:name="com.example.AlarmReceiver" />
            </intent-filter>

<intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>
4

1 に答える 1

1

AlarmReceiver.onReceive()オブジェクトを作成しNotificationますが、キューに入れることはありません。NotificationManager.notify()次のように呼び出す必要があります。

mNotificationManager.notify(MY_NOTIFICATION_ID, notification);

ここで、MY_NOTIFICATION_IDは任意の整数値です。後で通知をキャンセルする必要がある場合は、次のようにこの値を使用します。

mNotificationManager.cancel(MY_NOTIFICATION_ID);
于 2012-12-19T14:20:09.893 に答える