android中broadcastreceiver的用法-manifest中注册。
来源:互联网 发布:有什么好用的精华 知乎 编辑:程序博客网 时间:2024/06/10 18:38
package com.jinhoward.broadcast.activity;import com.jinhoward.broadcast.activity.R;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity{protected static final String MYACTION = "com.jinhoward.broadcast.ACTION";private Button btnBroadcast;@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);btnBroadcast=(Button)findViewById(R.id.btnBroadcast);btnBroadcast.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){Intent intent=new Intent().setAction(MYACTION);Log.i("MyReceiver", "按钮被点击");sendBroadcast(intent);}});}}
receiver类:
package com.jinhoward.broadcast.receiver;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;/** * @author jinhoward * @blog http://blog.csdn.net/jinhwoard */public class MyReceiver extends BroadcastReceiver{private static final String TAG = "MyReceiver";public MyReceiver(){Log.i(TAG, "MyReceiver的构造函数执行");}@Overridepublic void onReceive(Context context, Intent intent){Log.i(TAG, "接受到广播:"+intent.getAction());}}
manifest文件:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.jinhoward.broadcast.activity" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name" > <activity android:name="com.jinhoward.broadcast.activity.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="com.jinhoward.broadcast.receiver.MyReceiver" > <intent-filter> <action android:name="android.provider.Telephony.SMS_RECEIVED" /> </intent-filter> <intent-filter> <action android:name="com.jinhoward.broadcast.ACTION" /> </intent-filter> </receiver> </application> <uses-permission android:name="android.permission.RECEIVE_SMS" /></manifest>
学习到知识:
1、非系统类的广播需要自己定义action并且自己发送广播。
2、系统自带的广播不需要定义action,也不需要发送广播,只需要注册相应的系统广播事件,进行处理即可。
3、broadcastreceiver的生命周期
A BroadcastReceiver object is only valid for the duration of the call to onReceive(Context, Intent)
. Once your code returns from this function, the system considers the object to be finished and no longer active.
This has important repercussions to what you can do in an onReceive(Context, Intent)
implementation: anything that requires asynchronous operation is not available, because you will need to return from the function to handle the asynchronous operation, but at that point the BroadcastReceiver is no longer active and thus the system is free to kill its process before the asynchronous operation completes.
- android中broadcastreceiver的用法-manifest中注册。
- android中broadcastreceiver的用法-代码中注册
- Android中关于Manifest的权限注册
- Android应用开发中BroadcastReceiver的用法
- android的广播介绍,在manifest .xml中注册广播
- Android中关于Manifest的权限注册问题
- 【Android】Manifest中注册以内部类形式存在的Activity
- Android:在AndroidManifest中注册BroadcastReceiver的权限问题
- Android:在AndroidManifest中注册BroadcastReceiver的权限问题
- Android BroadcastReceiver的注册
- Android中BroadcastReceiver的介绍
- Android中BroadcastReceiver的使用
- Android中BroadcastReceiver的使用
- Android中BroadcastReceiver的介绍
- Android中BroadCastReceiver的使用
- Android中BroadcastReceiver的介绍
- Android中BroadcastReceiver的运用
- BroadcastReceiver在Service中注册
- 排序
- 【§超玄跑车win7电脑主题下载§系统美化】
- 《算法竞赛-训练指南》第一章-1.26_LA 3882
- 转载:2.6 服务进程Postgres
- Java中的递归原理分析
- android中broadcastreceiver的用法-manifest中注册。
- MVC3 类型“System.Web.Mvc.ModelClientValidationRule”同时存在
- 获取屏幕的高度和宽度DisplayMetrics
- div内容设float之后,div的自动高度适应
- hdu2159(贪心+01背包)
- 转2.6.1 初始化内存环境载:
- HDU 题目1698 Just a Hook(线段树)
- 不可摸数
- 基本数据结构:栈(stack)