AlarmManager不会火未决意图(AlarmManager wont fire pending

2019-10-19 06:31发布

我尝试使用AlarmManager与广播接收器。 我的问题是,我没有得到来自AlarmManager意图火灾。 林没有得到任何呼叫Log.i("test", "test")

这里是我的代码:

public void activityCheck()
    {
        Intent intent = new Intent(this.getApplicationContext(), com.example.workoutlog.UsageCheckService.class);   
        intent.putExtra("nofitication", "nofitication");

        PendingIntent service = PendingIntent.getService(this, 0, intent, 0);  

        AlarmManager m = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);  

         Calendar time = Calendar.getInstance();
         time.setTimeInMillis(System.currentTimeMillis());

         Log.i("Yo", "Yo");

    m.setRepeating(AlarmManager.RTC, time.getTimeInMillis(), 1000*60, service);     
    }

我的广播接收器类

public class UsageCheckService extends BroadcastReceiver {


        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub

            Log.i("test", "test");


        }

}

在我的清单

 <receiver android:name="com.example.workoutlog.UsageCheckService" >
        </receiver>

Answer 1:

如果手机处于睡眠状态时,你将不得不将其唤醒。

尝试这个:

一个小例子:

这是工作的代码。 它唤醒CPU每隔10分钟,直到电话关闭。

加入的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);
        }
    }
}


Answer 2:

如果设备处于“睡眠模式”(电池保存),广播也不会醒的。

您必须使用WakefulBroadcastReceiver

https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html



文章来源: AlarmManager wont fire pending intent