我正在尝试使用以下代码扩展活动来广播Toast消息.但是广播没有被另一个活动收到,烤面包没有显示.有人能解决我的错误吗?主要活动是SendBroadcast.
java
import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.View; public class SendBroadcast extends Activity { public static String BROADCAST_ACTION = "com.unitedcoders.android.broadcasttest.SHOWTOAST"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void sendBroadcast(View v) { Intent broadcast = new Intent(); broadcast.setAction(BROADCAST_ACTION); sendBroadcast(broadcast); } }
Toast Display Activity是ToastDisplay.java
import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.widget.Toast; public class ToastDisplay extends Activity { private BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context,Intent intent) { Toast.makeText(getApplicationContext(),"received",Toast.LENGTH_SHORT).show(); } }; @Override protected void onResume() { IntentFilter filter = new IntentFilter(); filter.addAction(SendBroadcast.BROADCAST_ACTION); registerReceiver(receiver,filter); super.onResume(); } @Override protected void onPause() { unregisterReceiver(receiver); super.onPause(); } }
和manifest.xml如下
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.broad" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="3" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".SendBroadcast" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".ToastReceiver" > <intent-filter> <action android:name="com.unitedcoders.android.broadcasttest.SHOWTOAST" /> </intent-filter> </receiver> </application> </manifest>
解决方法
可以有两种类型的broacast:静态和动态.静态是清单文件中声明的那些.动态可以在运行时声明.您将这两种类型的广播组合在一个广播中.
要声明一个简单的动态广播,您可以使用以下代码(基于您的代码).当显示活动时,它将只显示Toast消息.
import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Toast; public class BroadcastTestActivity extends Activity { public static String BROADCAST_ACTION = "com.unitedcoders.android.broadcasttest.SHOWTOAST"; BroadcastReceiver br = new BroadcastReceiver() { @Override public void onReceive(Context context,Intent intent) { Log.w("Check","Inside On Receiver"); Toast.makeText(getApplicationContext(),Toast.LENGTH_SHORT).show(); } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); IntentFilter filter = new IntentFilter(); filter.addAction(BROADCAST_ACTION); filter.addCategory(Intent.CATEGORY_DEFAULT); registerReceiver(br,filter); } @Override protected void onResume() { super.onResume(); sendBroadcast(); } @Override protected void onPause() { super.onPause(); unregisterReceiver(br); } public void sendBroadcast() { Intent broadcast = new Intent(); broadcast.setAction(BROADCAST_ACTION); broadcast.addCategory(Intent.CATEGORY_DEFAULT); sendBroadcast(broadcast); } }