开发者

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 s开发者_Go百科ervice (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. Where can be problem? Here are my codes:

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

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

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

    }
}


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

Just follow the sample code to get it working. You've messed few things with BroadcastReceiver and permissions.


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.

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. Just create a new one, e.g.:

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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜