android - 使用 BroadcastReceiver 自动启动服务不起作用?
问题描述
我想在设备重新启动时自动启动我的服务。请不要将此标记为Question的重复项,但给出的解决方案不起作用!
BootUpReceiver.java (广播接收器)
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import java.util.Map;
public class BootUpReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent pushIntent = new Intent(context, MyService.class);
context.startService(pushIntent);
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.shubham.servic">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:enabled="true"
android:name=".BootUpReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<service
android:name=".MyService"
android:enabled="true"
android:exported="true">
</service>
</application>
</manifest>
MainActivity.java
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;
public class MainActivity extends Activity {
String msg = "Android : ";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(msg, "The onCreate() event");
}
public void startService(View view) {
startService(new Intent(getBaseContext(), MyService.class));
}
// Method to stop the service
public void stopService(View view) {
stopService(new Intent(getBaseContext(), MyService.class));
}
}
但是服务仍然没有在重新启动时自行启动。
我还有一个不同的问题:
我必须在固定的时间间隔内运行我的后台服务,到目前为止,我已经创建了一个AlarmManager
内部服务本身,它在一段时间后一次又一次地调用相同的服务,我怎样才能实现“运行后台服务以更有效的方式以固定的时间间隔进行?”
解决方案
由于 Android Oreo 的限制,您无法在后台启动服务。有详细信息的来源
就像消息来源所建议的那样,您应该改用预定的作业。最简单的管理方法是使用android-job或workmanager 之类的库。Android Job 是 Evernote 开发的一个常用的 3rd 方库,但他们宣布支持将在仍处于 alpha 阶段的 Workmanager 投入生产一段时间后结束。
推荐阅读
- django - Django AWS S3 - 400“错误请求” - 无法从存储桶访问图像
- sql - 在数据库中我的查询得到第一行当我分组时
- python - 在 LinkedList 中两次插入同一个节点会触发无限循环
- php - 正则表达式指南仅在特殊条件下从字符串中删除数字
- windows - 检查特定进程在 delphi 中是否提升
- bash - 如何在 find 命令中使用文件名中的变量?
- c# - async 方法不异步运行
- html - 在 Wordpress 上嵌入 Airtable
- python - 如何使用 nlp 从问题列表中生成答案?
- javascript - 我的 javascript for 循环不运行。有什么问题