/**
* MainActivity
* @author zuolongsnail
*
*/
public class MainActivity extends Activity {
private Button sendStaticBtn;
private Button sendDynamicBtn;
private Button sendSystemBtn;
private static final String STATICACTION = "com.byread.static";
private static final String DYNAMICACTION = "com.byread.dynamic";
// USB设备连接
private static final String SYSTEMACTION = Intent.ACTION_POWER_CONNECTED;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sendStaticBtn = (Button) findViewById(R.id.send_static);
sendDynamicBtn = (Button) findViewById(R.id.send_dynamic);
sendSystemBtn = (Button) findViewById(R.id.send_system);
sendStaticBtn.setOnClickListener(new MyOnClickListener());
sendDynamicBtn.setOnClickListener(new MyOnClickListener());
sendSystemBtn.setOnClickListener(new MyOnClickListener());
}
class MyOnClickListener implements OnClickListener{
@Override
public void onClick(View v) {
// 发送自定义静态注册广播消息
if(v.getId() == R.id.send_static){
Log.e("MainActivity", "发送自定义静态注册广播消息");
Intent intent = new Intent();
intent.setAction(STATICACTION);
intent.putExtra("msg", "接收静态注册广播成功!");
sendBroadcast(intent);
}
// 发送自定义动态注册广播消息
else if(v.getId() == R.id.send_dynamic){
Log.e("MainActivity", "发送自定义动态注册广播消息");
Intent intent = new Intent();
intent.setAction(DYNAMICACTION);
intent.putExtra("msg", "接收动态注册广播成功!");
sendBroadcast(intent);
}
// 发送系统动态注册广播消息。当手机连接充电设备时会由系统自己发送广播消息。
else if(v.getId() == R.id.send_system){
Log.e("MainActivity", "发送系统动态注册广播消息");
Intent intent = new Intent();
intent.setAction(SYSTEMACTION);
intent.putExtra("msg", "正在充电。。。。");
}
}
}
@Override
protected void onStart() {
super.onStart();
Log.e("MainActivity", "注册广播事件");
// 注册自定义动态广播消息
IntentFilter filter_dynamic = new IntentFilter();
filter_dynamic.addAction(DYNAMICACTION);
registerReceiver(dynamicReceiver, filter_dynamic);
// 注册系统动态广播消息
IntentFilter filter_system = new IntentFilter();
filter_system.addAction(SYSTEMACTION);
registerReceiver(systemReceiver, filter_system);
}
private BroadcastReceiver dynamicReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.e("MainActivity", "接收自定义动态注册广播消息");
if(intent.getAction().equals(DYNAMICACTION)){
String msg = intent.getStringExtra("msg");
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}
}
};
private BroadcastReceiver systemReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.e("MainActivity", "接收系统动态注册广播消息");
if(intent.getAction().equals(SYSTEMACTION)){
String msg = intent.getStringExtra("msg");
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}
}
};
}
/**
* 自定义静态注册广播消息接收器
* @author zuolongsnail
*
*/
public class StaticReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String msg = intent.getStringExtra("msg");
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}
}
/**
* 系统静态注册广播消息接收器
*
* @author zuolongsnail
*
*/
public class SystemReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BATTERY_LOW)) {
Log.e("SystemReceiver", "电量低提示");
Toast.makeText(context, "您的手机电量偏低,请及时充电", Toast.LENGTH_SHORT).show();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.byread" android:versionCode="1" android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 注册自定义静态广播接收器 -->
<receiver android:name=".StaticReceiver">
<intent-filter>
<action android:name="com.byread.static" />
</intent-filter>
</receiver>
<!-- 注册系统静态广播接收器 -->
<receiver android:name=".SystemReceiver">
<intent-filter>
<action android:name="android.intent.action.BATTERY_LOW" />
</intent-filter>
</receiver>
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello" />
<Button android:id="@+id/send_static" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="发送自定义静态注册广播" />
<Button android:id="@+id/send_dynamic" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="发送自定义动态注册广播" />
<Button android:id="@+id/send_system" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="发送系统动态注册广播" />
</LinearLayout>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有