问题
I can't start a service as soon as device boots. My service and receiver classes are as follows.
package android_programmers_guide.BroadcastReceiver1;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import android.app.ActivityManager;
import android.app.Service;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MyService extends Service {
/**
* Delay until first exeution of the Log task.
*/
private final long mDelay = 0;
/**
* Period of the Log task.
*/
private final long mPeriod = 500;
/**
* Log tag for this service.
*/
private final String LOGTAG = "**BootDemoService**";
/**
* Timer to schedule the service.
*/
private Timer mTimer;
/**
* Implementation of the timer task.
*/
private class LogTask extends TimerTask {
public void run() {
Log.i(LOGTAG, "scheduled");
}
}
private LogTask mLogTask;
@Override
public IBinder onBind(final Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.i(LOGTAG, "created");
mTimer = new Timer();
mLogTask = new LogTask();
}
@Override
public void onStart(final Intent intent, final int startId) {
super.onStart(intent, startId);
Log.i(LOGTAG, "started");
mTimer.schedule(mLogTask, mDelay, mPeriod);
}
}
This is receiver class.
package android_programmers_guide.BroadcastReceiver1;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyStartupIntentReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent bootintent) {
Intent mServiceIntent = new Intent();
mServiceIntent.setAction("android_programmers_guide.BroadcastReceiver1.MyService");
context.startService(mServiceIntent);
}
}
And this is manifest file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android_programmers_guide.BroadcastReceiver1"
android:versionCode="1" android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<service android:name=".MyService">
<intent-filter>
<action android:name="android_programmers_guide.BroadcastReceiver1.MyService">
</action>
</intent-filter>
</service>
<receiver android:name=".MyStartIntentReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED">
</action>
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="4" />
Please reply ASAP and thanks in advance. Cheers!
PS: I only copy code from a tutorial and I am using Android Gingerbread with Eclipse Galileo.
After editing as you guys told, my service is succeed. But there's still a problem. I can't start an activity. My code is as follows:
public void onStart(final Intent intent, final int startId)
{
super.onStart(intent, startId);
Intent intent1 = new Intent();
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent1);
}
回答1:
Please add that line in manifest.
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
After Edit:
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent pushIntent = new Intent(context,MyService.class);
context.startService(pushIntent);
}
}
回答2:
Your problem seems to be in AndroidManifest.xml
First of all you have mis-spelled your **MyStartupIntentReceiver**
class here
<receiver android:name=".MyStartIntentReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED">
</action>
</intent-filter>
</receiver>
1.) First of all change the above thing and check it.
2.) Else remove the <intent-filter> ...</intent-filter>
for your service class
It should be only
<service android:enabled="true" android:name=".MyService" />
Hope you will surely get it done after doing this.
来源:https://stackoverflow.com/questions/7116877/cant-complete-after-boot-listener-in-android