2

デバイスを再起動した後、アプリの実行を (バックグラウンドで) 開始する必要があります。これが私が今まで思いついたことです(ここから多くの助けを借りた後...)

これは、broadcastreceiver を使用する私の BootUpReceiver です。

public class BootUpReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent serviceIntent = new Intent(context, RebootService.class);
        serviceIntent.putExtra("caller", "RebootReceiver");
        context.startService(serviceIntent);
    }
}

これはサービス クラスです。

public class RebootService extends IntentService{

    public RebootService(String name) {
        super(name);
        // TODO Auto-generated constructor stub
}

protected void onHandleIntent(Intent intent) {

        Intent i = new Intent(getBaseContext(), MainActivity.class);  
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        String intentType = intent.getExtras().getString("caller");
        if(intentType == null) 
            return;
        if(intentType.equals("RebootReceiver")) 
            getApplication().startActivity(i);            
    }
}

これは私のアンドロイドマニフェストです:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <receiver
        android:name=".BootUpReceiver"
        android:enabled="true"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>

    <service android:name=".RebootService"/>
</application>

問題は、これを携帯電話にインストールして再起動すると、アプリがクラッシュすることです。「転送が機能しなくなりました」と表示されます。OKボタンを押してアプリ情報を確認すると、アプリが起動しています。

私はアンドロイドが初めてで、何が起こっているのかわかりません。さらに権限を追加する必要がありますか?

親切に助けてください。ティア

4

1 に答える 1

0

あなたの問題は RebootService コンストラクターにあると思います。システムがそれを呼び出すと、引数が提供されないため、クラッシュします。ログを見ると、おそらく「サービスをインスタンス化できません...」という結果が表示されます。

コンストラクターを次のものに置き換えてみてください。

public RebootService() {
    super( "Reboot Service" );
}
于 2012-10-14T16:41:34.130 に答える