0

デバイスの起動時にサービスを開始しようとしています。私は多くのことを検索し、手順に従いましたが、まだうまくいきません. モバイルが再起動すると、最初に「アプリケーションが予期せず停止しました」と表示されます。レシーバーファイルからのものであることは知っていますが、特に問題がどこにあるのかわかりませんでした。

レシーバー.java

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class Receiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent arg1) {
        // TODO Auto-generated method stub
        if(arg1.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
        {
            Intent myIntent = new Intent();
            myIntent.setAction("com.androidhive.jsonparsing.UpdateService");
            context.startService(myIntent);
        }
    }   
}

Androidマニフェスト

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:label="@string/app_name"
        android:name=".AndroidJSONParsingActivity" >
        <intent-filter >
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <!-- Single List Item View -->
    <activity
        android:label="Single Menu Item"
        android:name=".SingleMenuItemActivity" >
    </activity>
    <service android:name=".UpdateService">
        <intent-filter>
            <action android:name="com.androidhive.jsonparsing.UpdateService"/>
        </intent-filter>
    </service>
    <receiver android:name=".MyReceiver" 
        android:enabled="true"
        android:exported="false">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
        </intent-filter>
    </receiver>
</application>

サービスを完全に開始するために追加する必要があるものはありますか。

4

3 に答える 3

0

レシーバーを次のように宣言しますAndroidManifest.xml

 <receiver android:name=".Receiver" 
        android:enabled="true"
        android:exported="false">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
        </intent-filter>
    </receiver>

BroadcastReceiverクラスを持っているReceiver.classが、AndroidManifest.xmlで宣言している ためMyReceiver

于 2013-01-15T09:20:55.877 に答える
0

使用する代わりに

Intent myIntent = new Intent();
myIntent.setAction("com.androidhive.jsonparsing.UpdateService");
context.startService(myIntent);

代わりにレシーバーでこれを試してください

Intent myIntent = new Intent(context, UpdateService.class);
context.startService(myIntent);
于 2016-02-05T06:25:37.473 に答える
0

次のような Android マニフェストを確認してください。

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
    android:label="@string/app_name"
    android:name=".AndroidJSONParsingActivity" >
    <intent-filter >
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

<!-- Single List Item View -->
<activity
    android:label="Single Menu Item"
    android:name=".SingleMenuItemActivity" >
</activity>
<service android:name=".UpdateService">
    <intent-filter>
        <action android:name="com.androidhive.jsonparsing.UpdateService"/>
    </intent-filter>
</service>
<receiver android:name=".Receiver" 
    android:enabled="true"
    android:exported="false">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
    </intent-filter>
</receiver>

問題は、受信者の宣言にあります。android:name タグを確認してください。正しいクラス名が必要です。

于 2013-01-15T09:32:34.530 に答える