0

ブロードキャスト レシーバーを使用しようとすると、問題が発生します。

ターゲット: 次のスキーマ 1 で動作する 3 つのアプリがあります。1 つ目は、メッセージを受信したときにデータベースにデータを書き込むブロードキャスト レシーバー アプリです。2. 2 つ目は、データベースに保存する必要があるデータを含むインテントを送信するアプリ android です。3. 3 番目 - ホーム画面のウィジェットで、データベースに保存する必要があるデータを含むインテントも送信します。

というわけで、Eclipse上で3つのアプリを作っています。1. BroadcastReceiverExample - 次のファイルを持つ放送受信機

package com.test.receive;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

public class SimpleReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "service get started action", Toast.LENGTH_LONG).show();
        Log.e("START","START");

    }

}

およびマニフェスト ファイル ソース

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.test"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="7" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <receiver android:enabled="true" android:name=".receive.SimpleReceiver" android:exported="false">
            <intent-filter android:priority="999">
                <action android:name="com.test.SIMPLE_TEST_SERVICE"></action>
            </intent-filter>
        </receiver>

    </application>
</manifest>

また、Eclipseでアプリプロジェクト(BroadcastSenderExample)を作成し、次の送信者コードを含むファイルを持っています

package com.test.sender;

import com.test.R;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class BroadcastSenderExample extends Activity {

    public final static String ACTION="com.test.SIMPLE_TEST_SERVICE";
    public final static String TYPE="type";
    public final static int START=1;
    public final static int STOP=0;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btnStart=(Button)findViewById(R.id.btnStart);
        btnStart.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent bcIntent=new Intent(ACTION);
                sendBroadcast(bcIntent);
            }
        });
        btnEnd=(Button)findViewById(R.id.btnEnd);
        btnEnd.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent bcIntent=new Intent(ACTION);
                sendBroadcast(bcIntent);
            }
        });
    }

    private Button btnStart=null;
    private Button btnEnd=null;

}

次に、最初のアプリをデバイスにインストールし (エミュレーターも試します)、2 つ目のアプリをインストールします。そして、2 番目のアプリ実行インテント コールは何も起こりません。

私は何を間違っていますか?

次のコードで 2 つのプロジェクトを作成します

プロジェクト 1 wBRReceiver

ファイルWBRReceiver.java

パッケージcom.x.brreceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

public class WBRReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        Log.i("THIS IS A TEST RECEIVER","THIS IS A TEST RECEIVER");
        Toast.makeText(arg0, "this is a test receiver", Toast.LENGTH_LONG).show();
    }

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.x.brreceiver"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <receiver android:name="WBRReceiver">
            <intent-filter>
                <action android:name="com.x.START"></action>
            </intent-filter>
        </receiver>

    </application>
</manifest>

とプロジェクト 2 wBRSender

ファイルWBRSenderActivity.java

package com.x.brsender;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class WBRSenderActivity extends Activity {

    private String ACTION_NAME="com.x.START";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Intent brr=new Intent(ACTION_NAME);
        //I can't use this
        //brr.setClass(this, WBRReceiver.class);
        //Because i just don't have this class in this case
        sendBroadcast(brr);
    }
}

そしてマニフェスト

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.x.brsender"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

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

    </application>
</manifest>

次に、最初のアプリをエミュレーターにインストールしてから、2 つ目のアプリを実行します。そして、それは機能します。

4

1 に答える 1

1

logcat の出力を見ましたか? 何が問題なのかを正確に教えてくれる可能性は非常に高いです。

コードをあまり凝視しなくても、マニフェストが壊れているようです。あなたのレシーバーでは、android:name は ".receive.SimpleReceiver" であると述べています... この値 (. で始まる場合) は、単に "Android パッケージ名に続く部分) ではありません。ただし、そのように機能しますあなたの場合、あなたの Android パッケージは "com.test" ですが、レシーバーを含むパッケージは "com.test.receive.SimpleReceiver" であり、その Java パッケージは "com.test.receive" です。 android:name を「com.test.receive.SimpleReceiver」に。

于 2011-08-29T11:08:44.327 に答える