怎么做微信上的网站吗,如果在网站做推广连接,公众号如何做网站,网站建设推广代运营Android Broadcast 广播 进程内本地广播 如果你是在你的应用之内使用广播#xff0c;即不需要跨进程#xff0c;考虑使用LocalBroadcastManager #xff0c;这样更有效率#xff08;因为不需要跨进程通信#xff09;#xff0c;并且你不用考虑一些其他应用可以发送或接收… Android Broadcast 广播 进程内本地广播 如果你是在你的应用之内使用广播即不需要跨进程考虑使用LocalBroadcastManager 这样更有效率因为不需要跨进程通信并且你不用考虑一些其他应用可以发送或接收你的广播相关的安全问题。 下面介绍更一般的方法。 广播的两种注册方法 广播有静态和动态两种注册方法 静态注册在AndroidManifest.xml中加上receiver 标签。 动态注册通过 Context.registerReceiver()方法进行注册。比如在onResume中注册在onPause中注销。 附上例子例子中的布局、MyReceiver类常量类都是相同的在前面列出 布局文件都一样 RelativeLayout xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:toolshttp://schemas.android.com/toolsandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:paddingBottomdimen/activity_vertical_marginandroid:paddingLeftdimen/activity_horizontal_marginandroid:paddingRightdimen/activity_horizontal_marginandroid:paddingTopdimen/activity_vertical_margintools:context.DemoBroadcastActivity TextViewandroid:idid/helloTextandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:textstring/hello_world /Buttonandroid:idid/sendBtnandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_belowid/helloTextandroid:textstring/send //RelativeLayout 自己写的Receiver类 import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;public class MyReceiver extends BroadcastReceiver
{public MyReceiver(){super();Log.d(AppConstants.LOG_TAG, Receiver constructor);}Overridepublic void onReceive(Context context, Intent intent){Log.d(AppConstants.LOG_TAG, onReceive);String message intent.getStringExtra(AppConstants.MSG_KEY);Log.i(AppConstants.LOG_TAG, message);Toast.makeText(context, Received! msg: message, Toast.LENGTH_SHORT).show();}} 应用常量 public class AppConstants
{public static final String LOG_TAG Broadcast;public static final String MSG_KEY msg;public static final String BROADCAST_ACTION com.example.demobroadcast.BroadcastAction;} 下面就是不同的部分了 静态注册的实例代码 静态注册是在manifest文件中进行: ?xml version1.0 encodingutf-8?
manifest xmlns:androidhttp://schemas.android.com/apk/res/androidpackagecom.example.demobroadcastandroid:versionCode1android:versionName1.0 uses-sdkandroid:minSdkVersion8android:targetSdkVersion17 /uses-permission android:nameandroid.permission.RECEIVE_SMS /uses-permission android:nameandroid.permission.SEND_SMS /applicationandroid:allowBackuptrueandroid:icondrawable/ic_launcherandroid:labelstring/app_nameandroid:themestyle/AppTheme activityandroid:namecom.example.demobroadcast.DemoBroadcastActivityandroid:labelstring/app_name intent-filter android:priority1000action android:nameandroid.intent.action.MAIN /category android:nameandroid.intent.category.LAUNCHER //intent-filter/activityreceiverandroid:namecom.example.demobroadcast.MyReceiverintent-filter action android:namecom.example.demobroadcast.BroadcastAction //intent-filter/receiver/application/manifest 所以Java代码 package com.example.demobroadcast;import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;public class DemoBroadcastActivity extends Activity
{private Button sendBtn null;Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_demo_broadcast);sendBtn (Button) findViewById(R.id.sendBtn);sendBtn.setOnClickListener(new OnClickListener(){Overridepublic void onClick(View v){Intent intent new Intent();intent.setAction(AppConstants.BROADCAST_ACTION);intent.putExtra(msg, 圣骑士wind);sendBroadcast(intent);}});}} 动态注册的实例代码: 动态注册是在Java代码中进行 package com.example.demobroadcast2;import com.example.demobroadcast.R;import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;public class DemoBroadcastActivity extends Activity
{private Button sendBtn null;private MyReceiver mReceiver;Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_demo_broadcast);sendBtn (Button) findViewById(R.id.sendBtn);sendBtn.setOnClickListener(new OnClickListener(){Overridepublic void onClick(View v){Intent intent new Intent();intent.setAction(AppConstants.BROADCAST_ACTION);intent.putExtra(msg, 圣骑士wind);sendBroadcast(intent);}});}Overrideprotected void onResume(){super.onResume();mReceiver new MyReceiver();IntentFilter intentFilter new IntentFilter(AppConstants.BROADCAST_ACTION);registerReceiver(mReceiver, intentFilter);}Overrideprotected void onPause(){super.onPause();unregisterReceiver(mReceiver);}Overrideprotected void onDestroy(){super.onDestroy();}} 所以Manifest文件中不需要添加标签正常就行。 两种广播 Normal broadcasts 通过 Context.sendBroadcast发送完全是异步的asynchronous。所有的接收器以不确定的顺序运行通常是同时。 这样更有效率但是也意味着接收器不能传递结果也不能退出广播。 Ordered broadcasts 通过 Context.sendOrderedBroadcast发送。一次只向一个接收器发送。 由于每个接收器按顺序执行它可以向下一个接收器传递结果也可以退出广播不再传递给其他接收器。 接收器运行的顺序可以通过 android:priority 属性来控制相同优先级的接收器将会以随机的顺序运行。 接收器的生命周期 一个BroadcastReceiver的对象只在 onReceive(Context, Intent)被调用的期间有效一旦从这个方法返回系统就认为这个对象结束了不再活跃。 这对你在onReceive中能做什么有很大的影响不能做任何需要的操作anything that requires asynchronous operation is not available。 因为你需要从方法返回去进行你的异步操作而返回时BroadcastReceiver的对象已经不再活跃了系统可以在异步操作完成前任意杀死它的进程。 特别地不可以在BroadcastReceiver中显示对话框或者绑定一个service前者应该用 NotificationManager后者应该用Context.startService()。 参考资料 官方文档BroadcastReceiver http://developer.android.com/reference/android/content/BroadcastReceiver.html LocalBroadcastManager http://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html Training: Manipulating Broadcast Receivers On Demand http://developer.android.com/training/monitoring-device-state/manifest-receivers.html receiver标签 http://developer.android.com/guide/topics/manifest/receiver-element.html 转载于:https://www.cnblogs.com/mengdd/archive/2013/06/14/3135431.html