
朋友们,我无法将广播从一个活动发送到其他活动请参阅下面的代码并提供帮助:
public class Sendbroadcast extends Activity { public static String broADCAST_ACTION = "com.unitedcoders.androID.broadcasttest.SHOWTOAST";/* } });} public voID sendbroadcast(){ Intent broadcast = new Intent("com.unitedcoders.androID.broadcasttest.SHOWTOAST"); this.sendbroadcast(broadcast); //startActivity(broadcast);}}
接收方代码:
public class Toastdisplay extends Activity {private broadcastReceiver mReceiver;@OverrIDeprotected voID onResume() { // Todo auto-generated method stub super.onResume(); Log.i("!!!!!!!InchooTutorial@@@@@@@$$$$","%%%%%%% msg_for_me");耳鼻喉科
// String msg_for_me = intent.getStringExtra(“some_msg”);
//记录我们的消息值
Log.i(“!!!!!!! InchooTutorial @@@@@@ $$$$”,“%%%%%%% msg_for_me”);
} }; //registering our receiver this.registerReceiver(mReceiver, intentFilter);} @OverrIDe protected voID onPause() { // Todo auto-generated method stub super.onPause(); //unregister our receiver this.unregisterReceiver(this.mReceiver);}}Manifest.xml是:
<?xml version="1.0" enCoding="utf-8"?> <manifest xmlns:androID="http://schemas.androID.com/apk/res/androID" package="com.unitedcoders.androID.broadcasttest" androID:versionCode="1" androID:versionname="1.0"> <uses-sdk androID:minSdkVersion="4" /> <uses-permission androID:name="androID.permission.broADCAST_STICKY"/> <application androID:icon="@drawable/icon" androID:label="@string/app_name"> <activity androID:name=".Sendbroadcast" androID:label="@string/app_name"> <intent-filter> nitedcoders.androID.broadcasttest.SHOWTOAST" /> </application> </manifest>解决方法:
由于当您发送广播时其他活动未运行,您将不会收到它.
如果您希望即使活动未运行也要接收广播.在xml中声明它.
这是给你的代码.我希望这就是你想要的.
package com.pdd.Receiver;import androID.app.Activity;import androID.content.Intent;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.vIEw.VIEw.OnClickListener;import androID.Widget.button;public class ReceiverActivity extends Activity implements OnClickListener{ /** Called when the activity is first created. */ @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.main); button b=(button) findVIEwByID(R.ID.button1); b.setonClickListener(this); } public voID onClick(VIEw v) { // Todo auto-generated method stub Intent i =new Intent("com.pdd.receiver.myaction"); sendbroadcast(i); }}接收器类
package com.pdd.Receiver;import androID.content.broadcastReceiver;import androID.content.Context;import androID.content.Intent;import androID.Widget.Toast;public class MyReceiver extends broadcastReceiver { @OverrIDe public voID onReceive(Context arg0, Intent arg1) { // Todo auto-generated method stub //Intent i=new Intent(MyReceiver.class,Second.class); Intent i=new Intent(arg0,Second.class); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); arg0.startActivity(i); }}显示Toast的第二个活动
package com.pdd.Receiver;import androID.app.Activity;import androID.os.Bundle;import androID.Widget.Toast;public class Second extends Activity { @OverrIDe protected voID onCreate(Bundle savedInstanceState) { // Todo auto-generated method stub super.onCreate(savedInstanceState); Toast.makeText(getApplicationContext(), "This is second activity", 5000).show(); }}清单文件
<?xml version="1.0" enCoding="utf-8"?><manifest xmlns:androID="http://schemas.androID.com/apk/res/androID" package="com.pdd.Receiver" androID:versionCode="1" androID:versionname="1.0" > <uses-sdk androID:minSdkVersion="8" /> <application androID:icon="@drawable/ic_launcher" androID:label="@string/app_name" > <receiver androID:name="com.pdd.Receiver.MyReceiver"> <intent-filter> <action androID:name="com.pdd.receiver.myaction"></action> </intent-filter> </receiver> <activity androID:name=".ReceiverActivity" androID:label="@string/app_name" > <intent-filter> <action androID:name="androID.intent.action.MAIN" /> <category androID:name="androID.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity androID:name=".Second"></activity> </application></manifest> 总结 以上是内存溢出为你收集整理的无法从活动发送广播:android全部内容,希望文章能够帮你解决无法从活动发送广播:android所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)