AlarmManager不会从Service中触发

时间:2014-06-29 13:23:22

标签: android service notifications alarmmanager

所以我想每隔60秒重复一次警报(用于演示目的)。
我向BroadcastReceiver发出一个信号,然后启动NotificationService。
由于我的Android(4.4 / Kitkat)并不喜欢alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24 * 60 * 60* 1000, pendingIntent);方法,我试图从MainActivity设置闹钟,然后再从Notification Service内部设置闹钟(以便闹钟)每次触发AlarmManager时都会启动)。我的样本如下:

我的 MainActivity 在onCreate中调用alarmMethod:

public void alarmMethod(){

    Calendar calendar = Calendar.getInstance();

     calendar.set(Calendar.SECOND, 0);
     calendar.set(Calendar.MINUTE, 35);
     calendar.set(Calendar.HOUR, 9);
     calendar.set(Calendar.AM_PM, Calendar.AM);

      Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);

      pendingIntent = PendingIntent.getBroadcast(Vertreter.this, 0, myIntent,0);

      AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

      alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
    Toast.makeText(context, "Alarm started", Toast.LENGTH_SHORT).show();

}

MyReceiver:

package de.madstar.asg_erfurtvertretungsplan;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class MyReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
       Intent service1 = new Intent(context, NotifyService.class);

   context.startService(service1);


}   
}

NotificationService:

package de.madstar.asg_erfurtvertretungsplan;

import android.app.AlarmManager;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class NotifyService extends Service {

private PendingIntent pendingIntent2;
final Context context = this;


@Override
public IBinder onBind(Intent intent) {
    return null;
}


@Override
public void onCreate(){
    Toast.makeText(context, "This is a PopUp", Toast.LENGTH_SHORT).show();
    //Call AlarmManager again
    alarmMethod();
   }


public void alarmMethod(){

      Intent myIntent2 = new Intent(context, NotifyService.class);
      pendingIntent2 = PendingIntent.getBroadcast(context, 0, myIntent2,0);

      AlarmManager alarmManager2 = (AlarmManager)getSystemService(ALARM_SERVICE);

      alarmManager2.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 60 * 1000, pendingIntent2);

    Toast.makeText(context, "Alarm 2 started", Toast.LENGTH_SHORT).show();

}

}

清单

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.madstar.asg_erfurtvertretungsplan"
android:versionCode="1"
android:versionName="1.3" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />
 <uses-permission android:name="android.permission.SET_ALARM"></uses-permission>
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="de.madstar.asg_erfurtvertretungsplan.Vertreter"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service android:name=".MyAlarmService"
             android:enabled="true" />
    <service android:name=".NotifyService"
             android:enabled="true" />


    <receiver android:name=".MyReceiver"/>
</application>

</manifest>


所以我的问题是来自NotifyService的警报被调用(我可以看到Toast&#34;警报2开始&#34;)但是在 60 秒之后它没有被触发...... 也许重要的是,这个应用程序必须在 Android 2.3到4.4 上运行

如果有人能告诉我如何为Android 2.3设置工作AlarmManager.setRepeating()直到Android 4.4,我的问题也会得到解决。

提前致谢 - MadTracki

1 个答案:

答案 0 :(得分:0)

所以我终于让它与这个星座合作:

public void alarmMethod(){
        Calendar calendar = Calendar.getInstance();

        calendar.set(Calendar.HOUR_OF_DAY, 9);
        calendar.set(Calendar.MINUTE, 36); 
          final AlarmManager am = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
          Intent intent = new Intent(this, MyReceiver.class);
          PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
          if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
              am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);
              //Toast.makeText(context, "<4.4 Alarm start", Toast.LENGTH_SHORT).show();
          } else {
              setAlarmFromKitkat(am, calendar.getTimeInMillis(), pi);
          }
    }

@TargetApi(19)
    private void setAlarmFromKitkat(AlarmManager am, long ms, PendingIntent pi){
        am.setRepeating(AlarmManager.RTC_WAKEUP, ms, AlarmManager.INTERVAL_DAY, pi);
        //Toast.makeText(context, "4.4 Alarm start", Toast.LENGTH_SHORT).show();
    }