【问题标题】:Android BroadcastReceiver, auto run service after reboot of deviceAndroid BroadcastReceiver,设备重启后自动运行服务
【发布时间】:2013-01-01 08:31:26
【问题描述】:

你好,我正在写一个应用程序,当手机重启时,服务会自动启动,而不是点击应用程序。

这是我的代码

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>

我希望服务在重启后自动启动,而不是手动启动。

【问题讨论】:

  • 让你检查我的解决方案!

标签: android service broadcastreceiver manifest reboot


【解决方案1】:

首先这样做

1) 在您的&lt;manifest&gt; 元素中:

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

【讨论】:

  • Dixit,它有效!谢谢。你知道我是否想避免用户杀死服务,它会通过使用警报或某种触发器来激活它来重新启动服务?
  • @BangBoat 服务在工作完成后会自动关闭,并且服务应该在其工作完成时通过调用 stopSelf() 自行停止,或者其他组件可以通过调用 stopService() 来停止它。跨度>
  • 我想用我的应用程序做的是。当手机被盗时,更换 SIM 卡,该服务将比较 SIM 序列号,如果不同,服务/应用程序将向所有者发送电子邮件。我只是迷失了下一步该做什么,现在我的服务将在每次重新启动设备后启动。
  • 链接已损坏。
【解决方案2】:

您忘记了权限

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

【讨论】:

    【解决方案3】:

    尽管上述所有答案都是正确的,但是从Android Oreo 他们对后台服务设置了一些限制。

    查看Background Execution Limits 以了解有关 Android O 中后台限制的更多信息。

    当应用程序在后台时,您不能直接从 BroadCastReceiver 启动服务。

    但是,您可以通过调用startForegroundService() 或使用JobIntentService 从接收方启动前台服务,因为 JobIntentService 没有此类限制。

    【讨论】:

    • 我无法用前台服务实现,你介意用例子解释一下
    • 就是说和奥利奥一样,后台重启的时候不能启动broadcastreceiver服务吧?有什么解决办法吗?
    【解决方案4】:

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

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

    【讨论】:

      【解决方案5】:

      也试试这些权限,它可能对你有帮助。如果你使用的是 htc 手机,那么这些权限是必需的

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多