简体   繁体   English

在Android应用程序中自动启动服务

[英]autostart of service in android app

I am trying to develop simple app (service) which will start automatically on boot. 我正在尝试开发简单的应用程序(服务),它将在启动时自动启动。 With main activity is also created new service (I can see it in service task manager list). 主要活动也创建了新服务(我可以在服务任务管理器列表中看到它)。 This service should be also created after rebooting my phone (Samsung Galaxy Ace with android 2.3.4) without launching application , but it is not - I can't see it in service task manager list. 这个服务也应该在重新启动我的手机(Samsung Galaxy Ace with android 2.3.4)后创建而不启动应用程序,但它不是 - 我在服务任务管理器列表中看不到它。 Where can be problem? 哪里可以有问题? Here are my codes: 这是我的代码:

AndroidManifest.xml AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="cz.nafik.testService"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-sdk android:minSdkVersion="10" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".TestServiceActivity"
                  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:name=".MyService"
                 android:icon="@drawable/ic_launcher"
                 android:label="ledNotifier"
                 android:enabled="true">
            <intent-filter>
                <action android:name="cz.nafik.testService.MyService" />
            </intent-filter>
        </service>
        <receiver android:name=".BootUpReceiver" 
                  android:enabled="true"
                  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>     
    </application>
</manifest>

MyService.java MyService.java

package cz.nafik.testService;

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

public class MyService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        Toast.makeText(this, "service bind", Toast.LENGTH_LONG).show();
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "service starting", Toast.LENGTH_LONG).show();
        return super.onStartCommand(intent,flags,startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "service destroyed", Toast.LENGTH_LONG).show();
    }

}

BootUpReceiver.java BootUpReceiver.java

package cz.nafik.testService;

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

public class BootUpReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        intent.setAction("cz.nafik.testService.MyService");
        context.startService(intent);

    }

}

TestServiceActivity.java TestServiceActivity.java

package cz.nafik.testService;

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

public class TestServiceActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Intent serviceIntent = new Intent();
        serviceIntent.setAction("cz.nafik.testService.MyService");
        this.startService(serviceIntent);

    }
}

You have mixed up the receiver and the permissions in your manifest file. 您已将清单文件中的接收者和权限混淆了。

  • You should remove this line, at the beginning of the file: 您应该在文件的开头删除此行:

     <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
  • And you should change your receiver to this simpler version: 你应该将你的接收器更改为这个更简单的版本:

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

This way it should work, unless there is another problem on top of those explained above. 这样它应该有效,除非在上面解释的问题之上存在另一个问题。

I've asked almost the same question few days ago. 几天前我问了几乎同样的问题。 You can find it here: Android autostart application 你可以在这里找到它: Android自动启动应用程序

Just follow the sample code to get it working. 只需按照示例代码即可使其正常运行。 You've messed few things with BroadcastReceiver and permissions. 你使用BroadcastReceiver和权限搞砸了一些东西。

Try removing: 尝试删除:

android:permission="android.permission.RECEIVE_BOOT_COMPLETED"

and maybe also 也许还有

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

from your manifest declaration for BootUpReceiver . 来自您的BootUpReceiver的清单声明。

I did not find either of those necessary to do the same in my own application. 我没有找到在我自己的应用程序中做同样的事情。

Also, I don't know what the effects of re-using the BOOT_COMPLETED intent in your BootupReceiver code. 另外,我不知道在BootupReceiver代码中重用BOOT_COMPLETED意图会产生什么影响。 Just create a new one, eg: 只需创建一个新的,例如:

public class BootUpReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent si = new Intent();
        si.setAction("cz.nafik.testService.MyService");
        context.startService(si);
    }

}

I think the problem is with the intent you're using to start the service: 我认为问题在于你用来启动服务的意图:

    intent.setAction("cz.nafik.testService.MyService");
    context.startService(intent);

This will not start the service cz.nafik.testService.MyService but rather invoke the action "cz.nafix..." on the previous recipients of the intent. 这不会启动服务cz.nafik.testService.MyService ,而是在intent的先前收件人上调用操作“cz.nafix ...”。 You probably want to explicitly set the service's class as the recipient of a new intent, and make the action specific to that service. 您可能希望将服务的类显式设置为新intent的接收者,并使该操作特定于该服务。

Rather than trying to reuse the intent you've received as part of the broadcast, create a new intent and use that instead: 而不是尝试重用您作为广播的一部分收到的意图,而是创建一个新意图并使用它:

Intent startMyService = new Intent( context, MyService.class );
startMyService.setAction( "MAKE_TOAST" );
context.startService( startMyService );

Then in the service, you can check what action to perform: 然后在服务中,您可以检查要执行的操作:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if( "MAKE_TOAST".equals( intent.getAction() ) )
    {
        Toast.makeText(this, "service starting", Toast.LENGTH_LONG).show();
        return super.onStartCommand(intent,flags,startId);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM