与天地兮比寿,与日月兮齐光。这篇文章主要讲述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静态动态广播】
推荐阅读
- OpenCV4Android释疑:透析Android以JNI调OpenCV的三种方式(让OpenCVManager永不困扰)
- 10个合法下载Torrent的最佳网站(哪个最好())
- 10+最佳订单管理系统(OMS)软件合集(哪个最好())
- 10款最佳移动会计应用合集(适用于Android和iOS)
- Android的15个最佳离线音乐播放器应用合集
- Windows中监控游戏FPS的5款最佳软件合集
- 5款最佳游戏制作软件合集(哪一个最流行())
- 5款最佳家具设计软件合集(你不该错过的工具!)
- 5款最佳绿屏软件合集(哪个最好用())