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:チュートリアルからコードをコピーするだけで、EclipseGalileoでAndroidGingerbreadを使用しています。

皆さんが言ったように編集した後、私のサービスは成功しました。しかし、まだ問題があります。活動を開始できません。私のコードは次のとおりです。

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.)まず、上記を変更して確認してください。

2.)それ以外の<intent-filter> ...</intent-filter>場合は、サービスクラスのを削除します

それだけである必要があります

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

これを行った後、あなたが確実にそれを成し遂げることを願っています。

于 2011-08-19T05:13:08.987 に答える