gpt4 book ai didi

Android保持intentservice运行并监听广播

转载 作者:太空宇宙 更新时间:2023-11-03 13:53:03 26 4
gpt4 key购买 nike

我试图跟踪在用户未启动应用程序的情况下触发“SCREEN_ON”的次数。该应用程序本身仅显示一个带有一些图表和信息的 Activity 。我创建了一个小测试,但我认为这不是正确的方法,因为它会耗尽我的电池。

我得到了一个广播接收器“BOOT_COMPLETED”,它启动了一个粘性 IntentService,它正在注册“SCREEN_ON”广播接收器,它有一个永无止境的循环来捕获广播(电池耗尽问题)。

是否可以在没有服务的情况下收听“SCREEN_ON”广播?

list

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:name=".Application"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:hardwareAccelerated="true">

<activity
android:name=".activities.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:enabled="true" android:name=".services.ScreenOnService" />

<receiver android:name=".broadcast.receivers.AutoStartReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver android:name=".broadcast.receivers.ScreenOnReceiver">
<intent-filter>
<action android:name="android.intent.action.SCREEN_ON" />
</intent-filter>
</receiver>
</application>

自动启动接收器

public class AutoStartReceiver extends BroadcastReceiver
{
public void onReceive(Context aContext, Intent anIntent)
{
Log.i("[AutoStartReceiver]", "onReceive");
aContext.startService(new Intent(aContext, ScreenOnService.class));
}
}

ScreenOnReceiver

public class ScreenOnReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Log.i("[ScreenOnReceiver]", "onReceive");
}
}

屏幕开启服务

public class ScreenOnService extends IntentService
{
private ScreenOnReceiver theReceiver;

public ScreenOnService()
{
super(ScreenOnService.class.getName());
theReceiver = new ScreenOnReceiver();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.v("[ScreenOnService]", "onStartCommand");
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}

@Override
protected void onHandleIntent(Intent intent)
{
Log.i("[ScreenOnService]", "onHandleIntent");
registerReceiver(theReceiver, new IntentFilter(Intent.ACTION_SCREEN_ON));
while(true);
}

@Override
public void onDestroy()
{
Log.i("[ScreenOnService]", "onDestroy");
unregisterReceiver(theReceiver);
super.onDestroy();
}
}

最佳答案

与常规启动的 Service 相比,您是否有任何特殊原因要使用 IntentService

您应该能够使用常规启动的 Service 来实现此目的。将接收器注册为 onStartCommand 的一部分。

像这样:

public class MyService extends Service {
private ScreenOnReceiver mReceiver;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (mReceiver == null) {
mReceiver = new ScreenOnReceiver();
registerReceiver(mReceiver, new IntentFilter(Intent.ACTION_SCREEN_ON));
}
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}

// remember to unregister receiver in onDestroy...
}

这样可以避免繁忙的循环。 IntentService 被设计为执行后台操作的短期服务。您的使用不符合 IntentService 的目的。

关于Android保持intentservice运行并监听广播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32807014/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com