警报管理器是要走的路 - 棘手的部分是在警报管理器接收器返回后保持手机唤醒。所以
设置警报(请注意,您还应该注册一个“启动完成”接收器以在重新启动后设置警报 - 您的警报不会在重新启动后继续存在):
Intent monitoringIntent = new Intent(context, YourReceiver.class);
monitoringIntent.setAction("your action");
PendingIntent pi = PendingIntent.getBroadcast(context, NOT_USED,
monitoringIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager)
context.getSystemService(Context.ALARM_SERVICE);
// here is the alarm set up
am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + INITIAL_DELAY,
INTERVAL_BETWEEN_ALARMS, pi);
接收它 - 接收器拥有一个onReceive()
永不失败的 WakeLock:
public abstract class YourReceiver extends BroadcastReceiver {
@Override
final public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if ("your action".equals(action)) {
// monitoring - got broadcast from ALARM
try {
d("SS : " + new Signal().getSignalStrength(context));
} catch (InterruptedException e) {
e.printStackTrace();
}
// Actu8ally the lines above will ANR
// I did it with WakefulIntentService :
// WakefulIntentService.sendWakefulWork(
// context, YourWakefulService.class);
// Will be posting it asap
} else {
w("Received bogus intent : " + intent);
return;
}
}
}
如果您很幸运(yourRetrieveSignal() 足够快),这将起作用,否则您将需要在接收器中使用 (Wakeful)IntentService 模式。
WakefulIntentService将处理唤醒锁(如果您想避免依赖,请查看此处) - 编辑:请记住,您不能在意图服务中定义侦听器 - 请参见此处。
如果接收器 ANR 在您身上发生,您必须尝试 WakefulIntentService 模式。在任何一种情况下,您都可以使用它:
这实际上证明了最困难的部分:
class Signal {
static volatile CountDownLatch latch; //volatile is an overkill quite probably
static int asu;
private final static String TAG = Signal.class.getName();
int getSignalStrength(Context ctx) throws InterruptedException {
Intent i = new Intent(TAG + ".SIGNAL_ACTION", Uri.EMPTY, ctx,
SignalListenerService.class);
latch = new CountDownLatch(1);
asu = -1;
ctx.startService(i);
Log.d(TAG, "I wait");
latch.await();
ctx.stopService(i);
return asu;
}
}
在哪里 :
public class SignalListenerService extends Service {
private TelephonyManager Tel;
private SignalListener listener;
private final static String TAG = SignalListenerService.class.getName();
private static class SignalListener extends PhoneStateListener {
private volatile CountDownLatch latch;
private SignalListener(CountDownLatch la) {
Log.w(this.getClass().getName(), "CSTOR");
this.latch = la;
}
@Override
public void onSignalStrengthChanged(int asu) {
Signal.asu = asu;
latch.countDown();
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.w(TAG, "Received : " + intent.getAction());
Tel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
listener = new SignalListener(Signal.latch);
@SuppressWarnings("deprecation")
final int listenSs = PhoneStateListener.LISTEN_SIGNAL_STRENGTH;
Tel.listen(listener, listenSs);
return START_STICKY;
}
@Override
public void onDestroy() {
Log.w(TAG, "onDestroy");
Tel.listen(listener, PhoneStateListener.LISTEN_NONE);
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
这是工作代码(但不可否认,它不是优雅的顶峰 - 欢迎评论/更正)。不要忘记在清单中注册您的服务并获取权限。
编辑 2013.07.23:我没有使用onReceive
- 如果你使用它,它将 ANR - 如果你在其中使用 WakefulIntentServiceonReceive
并在那里调用,这是工作代码SignalListenerService
。