动态注册和注销BroadercasterReceive(二)
有时候不需要监听广播是否有消息,所以可以使用动态注册的方式。即不需要再androidmanifest.xml中注册。
主要代码如下:
package com.example.yabushan.broadercastreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyReceiver extends BroadcastReceiver {
//注意:这里是上边的包名+intent.action.MyRecerver(作为隐示Intent调用)
public final String ACTION="com.example.yabushan.broadercastreceiver.intent.action.MyReceiver";
public MyReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("接收到广播数据:"+intent.getStringExtra("DATA").toString());
}
}
package com.example.yabushan.broadercastreceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.deleverData).setOnClickListener(this);
findViewById(R.id.reg).setOnClickListener(this);
findViewById(R.id.unreg).setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.deleverData:
//这里需要使用隐示Intent
// Intent intent=new Intent(this,MyReceiver.class);
Intent intent=new Intent(myReceiver.ACTION);
intent.putExtra("DATA","您好");
sendBroadcast(intent);
break;
case R.id.reg:
if(myReceiver==null){
myReceiver =new MyReceiver();
registerReceiver(myReceiver,new IntentFilter(myReceiver.ACTION));
}
break;
case R.id.unreg:
if(myReceiver!=null){
//
unregisterReceiver(myReceiver);
}
break;
}
}
private MyReceiver myReceiver=null;
}
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.yabushan.broadercastreceiver" > <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView android:text="Hello World!" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="传递消息" android:id="@+id/deleverData" android:layout_centerVertical="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="注册" android:id="@+id/reg" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="注销" android:id="@+id/unreg" /> </LinearLayout>

浙公网安备 33010602011771号