在我的Android应用程序上,我试图每3分钟运行一次任务,如果CPU处于睡眠状态,那么它应该唤醒并运行我的任务;我的任务名为CheckTask,是一个Runnable。我知道AlarmManager是我需要的,我一直试图让它工作但是OnReceive()方法永远不会被执行。以下是相关代码,请告诉我有什么问题,以便我可以解决它并使其正常工作。
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.charles.sitechecker"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="18"
android:targetSdkVersion="18"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.FLASHLIGHT"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name="com.charles.sitechecker.MainActivity"
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=".LocalService"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="com.charles.sitechecker.LocalService" />
</intent-filter>
</service>
<receiver
android:name="com.charles.sitechecker.Alarm"
android:enabled="true"/>
</application>
Alarm.java
package com.charles.sitechecker;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class Alarm extends BroadcastReceiver
{
private MainActivity mainActivity;
private AlarmManager aManager;
public Alarm()
{
}
Alarm(MainActivity mainActivity)
{
this.mainActivity = mainActivity;
aManager = (AlarmManager)mainActivity.getSystemService(Context.ALARM_SERVICE);
}
void scheduleAlarm()
{
Intent intent = new Intent(mainActivity, Alarm.class);
PendingIntent pIntent = PendingIntent.getBroadcast(mainActivity, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
aManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, System.currentTimeMillis(), 180000, pIntent);
}
public void onReceive(Context context, Intent intent)
{
Runnable task = new CheckTask(mainActivity);
task.run();
}
}
LocalService.java
package com.charles.sitechecker;
import android.app.Notification;
import android.app.Service;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.IBinder;
public class LocalService extends Service
{
public void onCreate()
{
Notification n = MainActivity.checkTask.mainActivity.notifier.sendOngoingNotification("SiteChecker", "Sleeping", -1);
startForeground(-1, n);
Alarm alarm = new Alarm(MainActivity.checkTask.mainActivity);
alarm.scheduleAlarm();
}
public void onDestroy()
{
super.onDestroy();
}
public IBinder onBind(Intent intent)
{
return null;
}
public int onStartCommand(Intent intent, int flags, int startId)
{
return START_STICKY;
}
}
答案 0 :(得分:1)
在mainactivity中设置闹钟的示例类:
public void setRepeatingAlarm()
{
Intent intent = new Intent(this, ReceiveAlarm.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_CANCEL_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
(*time_in_milis to repeat*), pendingIntent);
}
广播接收器:
public class ReceiveAlarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, InService.class));
}
}
意图服务类示例:
public class InService extends IntentService
{
public InService() {
super("InService");
// TODO Auto-generated constructor stub
}
@Override
protected void onHandleIntent(Intent intent) {
//do your thing here
}
}
在标签内的清单
中声明您的广播接收器/服务类<receiver android:name="ReceiveAlarm" />
<service android:name="InService"></service>
试试这个。
答案 1 :(得分:0)
我认为你可能需要实现一个WakeLock。这需要特定权限,并允许您的应用“唤醒”设备以进行工作。
我使用了这个SO问题中提到的例子,当我实现我的闹钟(许多月前),它对我来说很有效。
AlarmManager and WakeLock example
具体示例链接是:(来自CommonsGuy) https://github.com/commonsguy/cw-omnibus/tree/master/AlarmManager/Wakeful