在屏幕关闭时接收事件和意图 Android
Receive event and intent while screen is off on Android
我需要一个服务来处理屏幕关闭时的事件,例如接收延迟消息(使用处理程序)和互联网连接状态更改。
服务是否有可能在不永久使用 PARTIAL_WAKE_LOCK 的情况下获得这些信号,因为我不需要一直到 运行 的服务?
您需要 BroadcastReceivers
才能接收不同的状态。有关详细信息,请参阅 Android 文档
http://developer.android.com/reference/android/content/BroadcastReceiver.html
还有例子,可以参考这里https://www.grokkingandroid.com/android-getting-notified-of-connectivity-changes/
link 提供的示例中的一些片段
registerReceiver(
new ConnectivityChangeReceiver(),
new IntentFilter(
ConnectivityManager.CONNECTIVITY_ACTION));
BroadcastReceiver
实施
public class ConnectivityChangeReceiver
extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
debugIntent(intent, "grokkingandroid");
}
private void debugIntent(Intent intent, String tag) {
Log.v(tag, "action: " + intent.getAction());
Log.v(tag, "component: " + intent.getComponent());
Bundle extras = intent.getExtras();
if (extras != null) {
for (String key: extras.keySet()) {
Log.v(tag, "key [" + key + "]: " +
extras.get(key));
}
}
else {
Log.v(tag, "no extras");
}
}
}
正如 StenSoft 所建议的那样,您可以将 AlarmManager
用于延迟消息或任何其他计划任务。我使用了下面的示例及其工作方式
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
}
}
对于延迟消息,请使用 alarm。
要在后台更改互联网连接,请使用 WakefulBroadcastReceiver
。
我需要一个服务来处理屏幕关闭时的事件,例如接收延迟消息(使用处理程序)和互联网连接状态更改。
服务是否有可能在不永久使用 PARTIAL_WAKE_LOCK 的情况下获得这些信号,因为我不需要一直到 运行 的服务?
您需要 BroadcastReceivers
才能接收不同的状态。有关详细信息,请参阅 Android 文档
http://developer.android.com/reference/android/content/BroadcastReceiver.html
还有例子,可以参考这里https://www.grokkingandroid.com/android-getting-notified-of-connectivity-changes/ link 提供的示例中的一些片段
registerReceiver(
new ConnectivityChangeReceiver(),
new IntentFilter(
ConnectivityManager.CONNECTIVITY_ACTION));
BroadcastReceiver
实施
public class ConnectivityChangeReceiver
extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
debugIntent(intent, "grokkingandroid");
}
private void debugIntent(Intent intent, String tag) {
Log.v(tag, "action: " + intent.getAction());
Log.v(tag, "component: " + intent.getComponent());
Bundle extras = intent.getExtras();
if (extras != null) {
for (String key: extras.keySet()) {
Log.v(tag, "key [" + key + "]: " +
extras.get(key));
}
}
else {
Log.v(tag, "no extras");
}
}
}
正如 StenSoft 所建议的那样,您可以将 AlarmManager
用于延迟消息或任何其他计划任务。我使用了下面的示例及其工作方式
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
}
}
对于延迟消息,请使用 alarm。
要在后台更改互联网连接,请使用 WakefulBroadcastReceiver
。