设备启动后我无法启动服务。 我的服务和接收器类如下。
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);
}
}
这是接收器类。
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);
}
}
这是明显的文件。
<?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" />
请尽快回复并提前致谢。 干杯!
PS:我只复制教程中的代码,而我正在使用Eclipse Gingerile与Eclipse Galileo。
按照你们的说法进行编辑后,我的服务成功了。 但仍然存在问题。 我无法开始活动。我的代码如下:
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);
}
答案 0 :(得分:3)
请在清单中添加该行。
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
编辑后:
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);
}
}