2011-08-29 162 views
0

尝试使用广播接收器时遇到一些问题。Android。广播接收器

目标: 我有三个应用程序,它将工作下一个架构 1.首先 - 广播接收器应用程序,它会写一些数据时,它会收到一条消息数据库。 2.第二 - 是应用程序的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 创建应用项目(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; 

} 

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

我在做什么错?

我就与下一个代码

项目一个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> 

而且项目中的两个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

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

没有太多地盯着你的代码,看起来你的清单已经坏了。在你的接收器中,你声明了android:name是“.receive.SimpleReceiver”...这个值(当以。开头时)不是简单的“跟随Android包名称的部分”) - 虽然它以这种方式工作在你的情况下,你的Android包是“com.test”,但是包含你的接收者的包是“com.test.receive”。SimpleReceiver“和它的Java包是”com.test.receive“。试着将你的android:name改为”com.test.receive.SimpleReceiver“。