0

设备启动后,我无法启动服务。我的服务和接收器类如下。

    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 Galileo 的 Android Gingerbread。

按照你们所说的编辑后,我的服务是成功的。但是还是有问题。我无法开始活动。我的代码如下:

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

2 回答 2

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

            } 

   }
于 2011-08-19T04:20:43.580 回答
1

您的问题似乎在 AndroidManifest.xml

首先你**MyStartupIntentReceiver**在这里拼错了你的班级

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

1.)首先改变上面的东西并检查它。

<intent-filter> ...</intent-filter>2.)否则为您的服务类删除

应该只是

 <service android:enabled="true" android:name=".MyService" />

希望你这样做后一定能完成。

于 2011-08-19T05:13:08.987 回答