android应用程序中的自动启动服务

android应用程序中的自动启动服务,android,android-manifest,android-service,Android,Android Manifest,Android Service,我正在尝试开发一个简单的应用程序(服务),它会在开机时自动启动。主活动还创建了新服务(我可以在服务任务管理器列表中看到)。在重新启动我的手机(三星Galaxy Ace搭载android 2.3.4)而不启动应用程序后,也应该创建此服务,但事实并非如此——我无法在服务任务管理器列表中看到它。问题出在哪里?这是我的密码: AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:androi

我正在尝试开发一个简单的应用程序(服务),它会在开机时自动启动。主活动还创建了新服务(我可以在服务任务管理器列表中看到)。在重新启动我的手机(三星Galaxy Ace搭载android 2.3.4)而不启动应用程序后,也应该创建此服务,但事实并非如此——我无法在服务任务管理器列表中看到它。问题出在哪里?这是我的密码:

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>
bootupureceiver.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

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);

    }
}

您混淆了清单文件中的接收者和权限

  • 您应该删除文件开头的这一行:

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    
    
    
  • 您应该将接收器更改为更简单的版本:

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

这种方法应该是可行的,除非在上面解释的问题之外还有其他问题。

几天前我问了几乎相同的问题。你可以在这里找到它:

只需按照示例代码进行操作即可。您在BroadcastReceiver和权限方面遇到了一些问题。

请尝试删除:

android:permission="android.permission.RECEIVE_BOOT_COMPLETED"
也许还有

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

我认为问题在于您用于启动服务的意图:

    intent.setAction("cz.nafik.testService.MyService");
    context.startService(intent);
这将不会启动服务
cz.nafik.testService.MyService
,而是调用意图的先前接收者上的操作“cz.nafix…”。您可能希望显式地将服务的类设置为新意图的接收者,并使操作特定于该服务

与其尝试重复使用您在广播中收到的意图,不如创建一个新的意图并使用它:

Intent startMyService = new Intent( context, MyService.class );
startMyService.setAction( "MAKE_TOAST" );
context.startService( startMyService );
然后在服务中,您可以检查要执行的操作:

@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);
    }
}

我的手机已根目录,操作系统会在可能的情况下自动将新应用程序安装到SD卡中。这就是我的问题,所以我不得不将android:installLocation=“internalOnly”放入AndroidManifest.xml的manifest标记中。谢谢你的建议。
Intent startMyService = new Intent( context, MyService.class );
startMyService.setAction( "MAKE_TOAST" );
context.startService( startMyService );
@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);
    }
}