我也尝试在Main.java中做的一切,如下,但不知道如何在清单文件中定义…
Main.java:
public class Main extends Activity {/** Called when the activity is first created. */ public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.main);String rec_data = "nothing Received";private broadcastReceiver myReceiver = new broadcastReceiver() { public voID onReceive(Context context,Intent intent) { // Todo auto-generated method stub if( intent.getStringExtra("send_data")!=null) rec_data = intent.getStringExtra("send_data"); Log.d("Received Msg : ",rec_data); } }; } protected voID onResume() { IntentFilter intentFilter = new IntentFilter(); //intentFilter.addDataType(String); registerReceiver(myReceiver,intentFilter); super.onResume(); } @OverrIDeprotected voID onPause() {// Todo auto-generated method stub super.onPause(); this.unregisterReceiver(this.myReceiver);}}
如果我不能像上面那样在一个类中做任何事情,那我该如何从Main.java调用broadcast Receiver?有谁能让我知道我在做错了什么?谢谢!
解决方法 使用这个为什么要发送自定义广播:定义动作名称:
public static final String broADCAST = "PACKAGE_name.androID.action.broadcast";
AndroIDManifest.xml注册接收器:
<receiver androID:name=".myReceiver" > <intent-filter > <action androID:name="PACKAGE_name.androID.action.broadcast"/> </intent-filter> </receiver>
注册会员:
IntentFilter intentFilter = new IntentFilter(broADCAST);registerReceiver( myReceiver,intentFilter);
从您的活动发送广播:
Intent intent = new Intent(broADCAST); Bundle extras = new Bundle(); extras.putString("send_data","test"); intent.putExtras(extras); sendbroadcast(intent);
你的broadcastReceiver:
private broadcastReceiver myReceiver = new broadcastReceiver() { public voID onReceive(Context context,Intent intent) { // Todo auto-generated method stub Bundle extras = intent.getExtras(); if (extras != null){ { rec_data = extras.getString("send_data"); Log.d("Received Msg : ",rec_data); } } };
有关Custom broadcast的更多信息,请参阅Custom Intents and Broadcasting with Receivers
总结以上是内存溢出为你收集整理的从Android中的活动开始播放收信人全部内容,希望文章能够帮你解决从Android中的活动开始播放收信人所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)