首页 > 代码库 > Android-broadcast静态动态广播

Android-broadcast静态动态广播

广播的静态和动态类型

静态广播:

1.继承之broadcastreceiver

public class MyStaticBroadcastReceiver extends BroadcastReceiver {    @Override    public void onReceive(Context context, Intent intent) {        //RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);        Toast.makeText(context,"zcxczcxstatic",Toast.LENGTH_SHORT).show();        Log.d("zcx","staticbroad");        //remoteViews.setTextViewText(R.id.widget_text, intent.getStringExtra("MESSAGE"));        //context.startActivity(intent);        AlertDialog.Builder builder = new AlertDialog.Builder(context);        builder.setTitle("提示")                .setMessage("收到BroadcastSend应用程序的广播")                .setPositiveButton("确定", new DialogInterface.OnClickListener() {                    @Override                    public void onClick(DialogInterface dialog, int which) {                    }                })                .setNegativeButton("取消", new DialogInterface.OnClickListener() {                    @Override                    public void onClick(DialogInterface dialog, int which) {                    }                });        AlertDialog dialog = (AlertDialog) builder.create();        dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);        dialog.show();                //AppWidgetManager.getInstance(context).updateAppWidget(new ComponentName(         //       context.getApplicationContext(), MyWidgetProvider.class), remoteViews);    }

 

2.在AndroidManifest中定义

<receiver android:priority="1000"  android:name=".MyStaticBroadcastReceiver"            android:permission="com.example.zcx.permission"            android:process=":remote"            >            <intent-filter>                <action android:name="android.zcx"></action>            </intent-filter></receiver>>

3.client 调用

Log.d("zcx","sendStaticBroadcastButton");                //Toast.makeText(getApplicationContext(),"zcxczcx",Toast.LENGTH_SHORT).show();                Intent intent = new Intent();                intent.setAction("android.zcx");                //intent.setAction("android.appwidget.action.zcx");                intent.addFlags(FLAG_INCLUDE_STOPPED_PACKAGES);                //intent.putExtra("MESSAGE", editText.getText().toString());                sendBroadcast(intent);

 技术分享

 

动态广播:

1.继承之broadcastreceiver

@Override    public void onReceive(Context context, Intent intent) {        //RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);        Log.d("zcx","MyDynamicBroadcastReceiver");        //remoteViews.setTextViewText(R.id.widget_text, intent.getStringExtra("MESSAGE"));        Toast.makeText(context,"zcxczcxDYnamic",Toast.LENGTH_SHORT).show();        //AppWidgetManager.getInstance(context).updateAppWidget(new ComponentName(         //       context.getApplicationContext(), MyWidgetProvider.class), remoteViews);        AlertDialog.Builder builder = new AlertDialog.Builder(context);        builder.setTitle("提示")                .setMessage("收到BroadcastSend dYNAMIC应用程序的广播")                .setPositiveButton("确定", new DialogInterface.OnClickListener() {                    @Override                    public void onClick(DialogInterface dialog, int which) {                    }                })                .setNegativeButton("取消", new DialogInterface.OnClickListener() {                    @Override                    public void onClick(DialogInterface dialog, int which) {                    }                });        AlertDialog dialog = (AlertDialog) builder.create();        dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);        dialog.show();    }

2.服务端代码注册启动

IntentFilter dynamic_filter = new IntentFilter();        dynamic_filter.addAction("android.appwidget.action.zcx");        myDynamicBroadcastReceiver = new MyDynamicBroadcastReceiver();        registerReceiver(myDynamicBroadcastReceiver,dynamic_filter);

3.客户端调用

public void onClick(View v) {                Intent intent = new Intent();                //intent.setAction("android.zcx");                intent.setAction("android.appwidget.action.zcx");                //intent.addFlags(FLAG_INCLUDE_STOPPED_PACKAGES);                //intent.putExtra("MESSAGE", editText.getText().toString());                sendBroadcast(intent);}

技术分享

 

 动态广播和静态广播的区别:

(1)动态注册广播不是常驻型广播,也就是说广播跟随Activity的生命周期。注意在Activity结束前,移除广播接收器。

静态注册是常驻型,也就是说当应用程序关闭后,如果有信息广播来,程序也会被系统调用自动运行。

 

这里采用动态和静态的实现方式,服务和客户端,服务要打开,客户才可以调用服务的广播接收,动态可以理解,静态为什么?

 

Android-broadcast静态动态广播