安卓广播接收器,设备重启后自动运行服务

37

你好,我正在编写一个应用程序。当手机重新启动时,服务将自动启动,而无需点击应用程序。

以下是我的代码:

BootCompleteReceiver.java

package com.example.newbootservice;

import android.content.BroadcastReceiver;  
import android.content.Context;  
import android.content.Intent;    

public class BootCompleteReceiver extends BroadcastReceiver {   

    @Override  
    public void onReceive(Context context, Intent intent) {  

        Intent service = new Intent(context, MsgPushService.class);  
        context.startService(service);   

    }  

}

MsgPushService.java

package com.example.newbootservice;

import android.app.Service;  
import android.content.Intent;  
import android.os.IBinder;   
import android.widget.Toast;

public class MsgPushService extends Service {  


    @Override  
    public void onCreate() {  
        super.onCreate();    
    }  

    @Override  
    public int onStartCommand(Intent intent, int flags, int startId) {  

        Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
        return Service.START_STICKY;  
    }  

    @Override
    public void onDestroy() {

        super.onDestroy();
        Toast.makeText(this, "Service Destroy", Toast.LENGTH_LONG).show();
    }

    @Override  
    public IBinder onBind(Intent arg0) {  
        return null;  
    }  
} 

MainActivity.java(不确定是否需要此文件)

package com.example.newbootservice;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        startService(new Intent(getBaseContext(), MsgPushService.class));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

清单

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.newbootservice"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <service android:name=".MsgPushService"/>

        <receiver android:name=".BootCompleteReceiver">  
            <intent-filter>     
                <action android:name="android.intent.action.BOOT_COMPLETED"/>  
                <category android:name="android.intent.category.DEFAULT" />  
            </intent-filter>  
        </receiver>

        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
我希望在重新启动后,服务能够自动启动,而不需要手动启动。
4个回答

10

你忘记了权限

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

8
尽管以上所有答案都是正确的,但从Android Oreo开始,他们对后台服务设置了一些限制。
请查看Android O中的背景执行限制以了解更多关于Android O中的后台限制信息。
当应用程序在后台运行时,您不能直接从BroadCastReceiver启动服务。
但是,您可以通过调用startForegroundService()来从接收器启动前台服务,或者使用JobIntentService,因为JobIntentService没有这种限制。

我无法使用前台服务实现,您介意给出一个示例来解释一下吗? - Ravi Kilnake
这意味着与Oreo相同,当应用程序在后台重新启动时,我们无法启动broadcastreceiver服务,对吗?有什么解决方法吗? - Prajwal Waingankar

2

在您的广播接收器类中使用此代码:

if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
   Intent service = new Intent(context, MsgPushService.class);  
    context.startService(service);  
  }

0

也可以尝试这些权限,它们可能会对您有所帮助。如果您正在使用HTC手机,则需要这些权限。

<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接