Android系统。广播接收器

时间:2011-08-29 10:52:54

标签: android background-application

尝试使用广播接收器时遇到了一些麻烦。

目标: 我有三个应用程序将工作下一个架构 1.首先 - 是广播接收器应用程序,它会在收到消息时将一些数据写入数据库。 2.第二 - 是app android,它将发送一些必须保存在数据库中的数据意图。 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中创建了App项目(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;

}

然后我在设备上安装第一个应用程序(并尝试模拟器),并安装第二个应用程序。 然后第二个app运行意图调用什么都没发生。

我做错了什么?

我用下一个代码制作了两个项目

投放一个wBRReceiver

文件 WBRReceiver.java

package 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>

并预测两个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>

然后我将第一个应用程序安装到模拟器上,然后运行第二个应用程序。它有效。

1 个答案:

答案 0 :(得分:1)

你看过logcat输出了吗?它很有可能告诉你到底出了什么问题。

如果没有太多地盯着你的代码,你的清单似乎就会被打破。在你的接收器中,你说android:name是“.receive.SimpleReceiver”......这个值(以。开头)不仅仅是“跟随Android包名称的部分” - 尽管它以这种方式运行在大多数情况下。在你的情况下,你的Android包是“com.test”,但是包含你的接收器的包是“com.test.receive.SimpleReceiver”,它的Java包是“com.test.receive”。尝试更改你的android:name to“com.test.receive.SimpleReceiver”。

相关问题