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