工作重复警报管理器不起作用,接收器无法启动

时间:2014-02-21 15:31:16

标签: android fragment alarmmanager

public void schedule()
{
    Long time = new GregorianCalendar().getTimeInMillis()+10*1000;
    Intent intent = new Intent(getActivity(), AlarmReceiver.class);
    AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
    alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, time, 10*1000, PendingIntent.getBroadcast(getActivity(), 1,  intent, PendingIntent.FLAG_UPDATE_CURRENT));
}

上面是我在片段中的方法。也在活动中尝试过。奇怪的是,这段代码适用于空的测试项目。我在这里看到的问题是,最后一部分alarmManager.setInexactRepeating()根本没有启动AlarmReceiver.class

我在这里做错了什么?

我的清单文件:

    <receiver android:name=".AlarmReceiver"/> 
    <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>

也是我的接收者:

public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent){
        Toast.makeText(context, "Alarm Triggered and SMS Sent", Toast.LENGTH_LONG).show();
    }
}

1 个答案:

答案 0 :(得分:7)

因为setInexactRepeating。使用setRepeating,它将在合适的时间处理。

而不是:

setInexactRepeating 

使用

setRepeating

setInexactRepeating,是操作系统和电池友好的,它将所有要完成的工作批量接收到报警接收并逐个工作,同时setRepeating立即触发报警

另请注意:重新启动电话后会擦除警报,您可能必须实施启动广播接收器以使其持久。确保你没有那个运行时,你需要在Manifest中实现它,当你的应用程序不在后台你不会收到任何广播。

一个小例子:

这是有效的代码。它会每隔10分钟唤醒一次CPU,直到手机关机。

添加到Manifest.xml:

...
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
...
<receiver  android:process=":remote" android:name="Alarm"></receiver>
...

代码:

    package YourPackage;
    import android.app.AlarmManager;
    import android.app.PendingIntent;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.os.PowerManager;
    import android.widget.Toast;

    public class Alarm extends BroadcastReceiver 
    {    
         @Override
         public void onReceive(Context context, Intent intent) 
         {   
             PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
             PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
             wl.acquire();

             // Put here YOUR code.
             Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); // For example

             wl.release();
         }

     public void SetAlarm(Context context)
     {
         AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
         Intent i = new Intent(context, Alarm.class);
         PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
         am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * 10, pi); // Millisec * Second * Minute
     }

     public void CancelAlarm(Context context)
     {
         Intent intent = new Intent(context, Alarm.class);
         PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
         AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
         alarmManager.cancel(sender);
     }
 }

从服务设置闹钟:

package YourPackage;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;

public class YourService extends Service
{
    Alarm alarm = new Alarm();
    public void onCreate()
    {
        super.onCreate();       
    }

    public void onStart(Context context,Intent intent, int startId)
    {
        alarm.SetAlarm(context);
    }

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

如果您想在电话启动时重复设置警报:

添加Manifest.xml的权限:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
...
<receiver android:name=".AutoStart">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"></action>
    </intent-filter>
</receiver>
...

创建新课程:

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

public class AutoStart extends BroadcastReceiver
{   
    Alarm alarm = new Alarm();
    @Override
    public void onReceive(Context context, Intent intent)
    {   
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
        {
            alarm.SetAlarm(context);
        }
    }
}