安卓 - 开机启动服务

131

从我在Stack Exchange和其他地方看到的一切来看,我已将一切正确设置以启动Android OS引导时的IntentService。不幸的是,它没有在引导时启动,并且我没有收到任何错误。也许专家们可以帮忙...

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.phx.batterylogger"
  android:versionCode="1"
  android:versionName="1.0"
  android:installLocation="internalOnly">

<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.BATTERY_STATS" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <service android:name=".BatteryLogger"/>
    <receiver android:name=".StartupIntentReceiver">  
        <intent-filter>  
            <action android:name="android.intent.action.BOOT_COMPLETED" />  
        </intent-filter>  
    </receiver>
</application>

</manifest>

开机自启的BroadcastReceiver:

package com.phx.batterylogger;

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

public class StartupIntentReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent serviceIntent = new Intent(context, BatteryLogger.class);
        context.startService(serviceIntent);
    }
}

更新:我尝试了下面提出的建议,还在StartupIntentReceiver的onReceive处理程序中添加了记录,例如Log.v("BatteryLogger", "Got to onReceive, about to start service");,但没有任何记录。所以它甚至没有到达BroadcastReceiver。

我认为我正在正确部署APK并进行测试,只是在Eclipse中运行Debug,控制台会成功将其安装到我的Xoom平板电脑上的\ BatteryLogger \ bin \ BatteryLogger.apk。然后,为了测试,我重新启动平板电脑,然后查看DDMS中的日志并检查操作系统设置中的运行服务。这一切都正确吗,还是我漏了什么?再次感谢任何帮助。


1
你遇到了什么问题,难道你没有得到任何用户界面吗? - Lalit Poptani
1
服务就是始终无法启动,这就是问题所在。 - Gady
1
你不需要日志来判断它是否在运行。Android操作系统会公开正在运行的服务。然而,最好使用日志来查看是否发生错误。如果出现错误,我猜测它会在context.startService()之前发生。 - Tony
1
我在onReceive处理程序中添加了Log.v("BatteryLogger", "Got to onReceive, about to start service");,但它从未出现在日志中。因此,监听器可能失败了(?)。 - Gady
为什么要编写广播接收器?你可以直接在接收到BOOT_COMPLETED意图时启动服务。只是一个想法。 - Vivek
显示剩余5条评论
7个回答

334

以下是一个完整的自启动应用程序示例。

AndroidManifest文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="pack.saltriver" android:versionCode="1" android:versionName="1.0">

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

    <application android:icon="@drawable/icon" android:label="@string/app_name">

        <receiver android:name=".autostart">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

        <activity android:name=".hello"></activity>
        <service android:enabled="true" android:name=".service" />
    </application>
</manifest>

autostart.java

public class autostart extends BroadcastReceiver 
{
    public void onReceive(Context context, Intent arg1) 
    {
        Intent intent = new Intent(context,service.class);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            context.startForegroundService(intent);
        } else {
            context.startService(intent);
        }
        Log.i("Autostart", "started");
    }
}

service.java

public class service extends Service
{
    private static final String TAG = "MyService";
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    public void onDestroy() {
        Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onDestroy");
    }

    @Override
    public void onStart(Intent intent, int startid)
    {
        Intent intents = new Intent(getBaseContext(),hello.class);
        intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intents);
        Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onStart");
    }
}

hello.java - 每次执行应用程序后,该文件将在启动设备时弹出。

public class hello extends Activity 
{   
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Toast.makeText(getBaseContext(), "Hello........", Toast.LENGTH_LONG).show();
    }
}

15
完整示例点赞。也许我应该将使用onHandleIntent的IntentService更改为普通的Service。 - Gady
6
这最终解决了问题。问题的原因是我的服务是IntentService而不是普通的Service,并且服务代码中存在错误。这个答案的详尽程度和Android中的日志记录帮助我解决了我的问题。 - Gady
3
+1 详细的例子。没有活动这样做是否也可行? - balas
2
onStart() 回调已经被弃用。您应该使用 onStartCommand() 代替。 - mmBs
1
@userAndroid 我不这么认为。从3.1版本开始,新安装的应用程序处于“停止状态”,不会接收广播或其他任何内容。我现在遇到了这个问题。我的应用程序不会在应用商店中出现,所以我想我会通过adb脚本安装并运行应用程序第一次。https://code.google.com/p/android/issues/detail?id=18225 - Bob
显示剩余16条评论

4
以下内容应该可以正常工作,我已经验证过了。也许你的问题出在其他地方。
接收者:
public class MyReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        if (Intent.ACTION_BOOT_COMPLETED.equals(arg1.getAction())) {
            Log.d("TAG", "MyReceiver");
            Intent serviceIntent = new Intent(context, Test1Service.class);
            context.startService(serviceIntent);
        }
    }
}

服务:

public class Test1Service extends Service {
    /** Called when the activity is first created. */
    @Override
    public void onCreate() {
        super.onCreate();
        Log.d("TAG", "Service created.");
    }
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("TAG", "Service started.");
        return super.onStartCommand(intent, flags, startId);
    }
    
    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        Log.d("TAG", "Service started.");
    }
    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }
}

清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.test"
      android:versionCode="1"
      android:versionName="1.0"
      android:installLocation="internalOnly">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
    
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.BATTERY_STATS" 
    />
<!--        <activity android:name=".MyActivity">
            <intent-filter>  
                <action android:name="android.intent.action.MAIN" /> 
                <category android:name="android.intent.category.LAUNCHER"></category> 
            </intent-filter>
       </activity> -->
        <service android:name=".Test1Service" 
                  android:label="@string/app_name"
                  >
        </service>
        <receiver android:name=".MyReceiver">  
            <intent-filter>  
                <action android:name="android.intent.action.BOOT_COMPLETED" /> 
            </intent-filter>  
        </receiver> 
    </application>
</manifest>

谢谢,但仍然无法正常工作。日志甚至没有显示它是否到达了StartupIntentReceiver。还有其他的想法吗? - Gady
1
你的代码在模拟器中能够正常运行吗?你能够在模拟器中接收到意图吗? - Vivek
我在模拟器中进行了测试,DDMS中出现了错误,但至少服务正在尝试启动,尽管我的Log()语句都没有出现在其中,并且模拟器设备在操作系统设置中也没有显示我的服务正在运行。以下是DDMS中的错误:System.err - at com.phx.batterylogger$1.onReceive(BatteryLogger.java:43) 这是否意味着问题出现在我BatteryLogger服务的第43行? - Gady

3

在设备启动后,由于设备进入睡眠状态,您的服务可能会在完成之前被关闭。您需要先获取一个唤醒锁。幸运的是,支持库提供了一个类来完成这个操作:

public class SimpleWakefulReceiver extends WakefulBroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // This is the Intent to deliver to our service.
        Intent service = new Intent(context, SimpleWakefulService.class);

        // Start the service, keeping the device awake while it is launching.
        Log.i("SimpleWakefulReceiver", "Starting service @ " + SystemClock.elapsedRealtime());
        startWakefulService(context, service);
    }
}

然后,在您的服务中,请确保释放唤醒锁:

    @Override
    protected void onHandleIntent(Intent intent) {
        // At this point SimpleWakefulReceiver is still holding a wake lock
        // for us.  We can do whatever we need to here and then tell it that
        // it can release the wakelock.

...
        Log.i("SimpleWakefulReceiver", "Completed service @ " + SystemClock.elapsedRealtime());
        SimpleWakefulReceiver.completeWakefulIntent(intent);
    }

不要忘记添加WAKE_LOCK权限:

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

1
我遇到了同样的问题。你介意帮我吗?谢谢!http://stackoverflow.com/questions/35373525/starting-my-service - Ruchir Baronia

3

我发现了一种方法,可以让你的应用程序在设备重启后正常运行,请按照以下步骤操作以获得成功。

AndroidManifest文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="pack.saltriver" android:versionCode="1" android:versionName="1.0">
    <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">

        <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:name=".UIBootReceiver" android:enabled="true" 
        android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>
         <service android:name=".class_Service" />
    </application>
</manifest>

UIBootReceiver

public class UIBootReceiver extends BroadcastReceiver {

private static final String TAG = "UIBootReceiver";

    @Override
    public void onReceive(Context context, Intent arg1)
    {
        Toast.makeText(context, "started", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(context,class_Service.class);
        context.startService(intent);
    }
  }

这是请求允许应用程序不需要管理电池节能,以便您可以在后台稳定运行。

在MainActivity类的onCreate()中声明此代码:

    Intent myIntent = new Intent();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) 
    {
        myIntent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
        myIntent.setData(Uri.parse("package:" + 
            DeviceMovingSpeed.this.getPackageName()));
    }
    startActivity(myIntent);

1

看起来与我的非常相似,但我使用接收者的完整包名:

<receiver android:name=".StartupIntentReceiver">

我有:

<receiver android:name="com.your.package.AutoStart"> 

1
嗯,解决你的问题有点困难,所以最好发布一个Hello World示例。 - Lalit Poptani
它应该适用于两种命名方案。 - Mister Smith

1

我已经成功了,没有使用完整的套装,你知道调用链在哪里被中断了吗?如果你使用Log()进行调试,在什么时候它不再工作?

我认为可能是在你的IntentService中,这看起来都很好。


我在onReceive处理程序中添加了Log.v("BatteryLogger", "Got to onReceive, about to start service");,但它从未出现在日志中。因此,监听器可能失败了(?)。 - Gady
广播接收器是如何在手机启动时自动启动的?谢谢! - Ruchir Baronia
小伙子,我已经好几年没碰安卓了。抱歉 @Ruchir - Phix

0

这并不是真的,正如Think Twice Code Once在同一个答案的评论部分所述。 - Ricardo A.
一个更详细的解释来支持我的观点:https://dev59.com/5GIj5IYBdhLWcg3wRTL3#46294732 - Ricardo A.
我已经很久没有做任何关于安卓的事情了,但你说得对,这是可能的。不需要其他论据。(我会编辑答案) - MrKew

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