动态广播的例子:
这个例子是接收系统网络变更的广播,如果系统的网络改变,那么就会弹出一个
Toast的对话框出来下面这个例子是
动态广播的例子:
源码这里下载:
关于动态广播的说明:
Android当中的广播就两种,一种是动态广播就是本文中的示例代码,另外一种就是
静态广播了,
动态广播的使用一定记得,
注册了就要记得释放掉!好借好还!
-
import android.content.BroadcastReceiver;
-
import android.content.Context;
-
import android.content.Intent;
-
import android.content.IntentFilter;
-
import android.support.v7.app.AppCompatActivity;
-
import android.os.Bundle;
-
import android.widget.Toast;
-
-
public class MainActivity extends AppCompatActivity {
-
-
private IntentFilter intentFilter;
-
private NetworkChanageReceiver networkChanageReceiver;
-
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_main);
-
intentFilter = new IntentFilter();
-
intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE"); //这个字符串是系统定义的,我们可以查阅帮助文档可以获得更多相关系统广播字符串!!!!!记得要添加相应的权限!!!!
-
NetworkChanageReceiver networkChanageReceiver = new NetworkChanageReceiver();
-
registerReceiver(networkChanageReceiver,intentFilter);
-
}
-
-
@Override
-
protected void onDestroy() {
-
super.onDestroy();
-
unregisterReceiver(networkChanageReceiver); //在当中活动当中释放掉我们注册的广播
-
}
-
-
class NetworkChanageReceiver extends BroadcastReceiver{
-
@Override
-
public void onReceive(Context context, Intent intent) {
-
Toast.makeText(MainActivity.this,"network chanage......",Toast.LENGTH_LONG).show();
-
}
-
}
-
}
静态广播的例子:
静态广播的是直接将接收广播的接收器直接写在Manifest文件当中去,和Activity写法差不多!只是在当中写的是要响应的广播代码,还有一点请注意权限问题,这里有涉及到系统相关的操作所以加上了:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 这个例子是接收系统启动广播的例子,将程序启动后,将模拟器重新启动,就可以看到效果:
源码这里下载:
AndroidManifest.xml 文件:
-
<?xml version="1.0" encoding="utf-8"?>
-
<manifest xmlns:android=""
-
package="barneyx.com.bootcompletereceiver">
-
-
<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>
-
-
<receiver android:name=".BootCompleteReceiver">
-
<intent-filter>
-
<action android:name="android.intent.action.BOOT_COMPLETED" />
-
</intent-filter>
-
</receiver>
-
</application>
-
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
-
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
-
</manifest>
-
BootCompleteReceiver.java文件:
-
package barneyx.com.bootcompletereceiver;
-
-
import android.content.BroadcastReceiver;
-
import android.content.Context;
-
import android.content.Intent;
-
import android.widget.Toast;
-
-
/**
-
* Created by Administrator on 2017/2/3.
-
*/
-
-
public class BootCompleteReceiver extends BroadcastReceiver {
-
@Override
-
public void onReceive(Context context, Intent intent) {
-
Toast.makeText(context,"Boot Complete ",Toast.LENGTH_LONG).show();
-
}
-
}
阅读(1198) | 评论(0) | 转发(0) |