1. if I want my application activity to catch broadcast. I must register the receiver and
set an intent filter which indicates the intent actions to be interested .
2. if I want my application activity to send a broadcast . No need to indicate the receiver.
It will run on the system level once some other apps registered the interested intent action
in their manifest .
Super crazy decouple system !
just set the action in the intent intent.setAction(Intent.ACTION_XXX);
and send the Broadcast with this intent Context.sendBroadcast(intent);
3. the receiver must extends BroadcastReceiver, and override the onReceive(Context context , Intent intent);
manifest:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.broadcast_test" android:versionCode="1" android:versionName="1.0"> <application android:label="@string/app_name" > <activity android:name="BroadcastTest" 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="TestReceiver"> <intent-filter> <action android:name="android.intent.action.EDIT"/> </intent-filter> </receiver> </application> <uses-sdk android:minSdkVersion="4"/> <uses-permission android:name="android.permission.RECEIVE_SMS"/> </manifest>
package com.broadcast_test; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; public class BroadcastTest extends Activity { LinearLayout.LayoutParams params; LinearLayout layout; Button sendButt; private OnClickListener sendListener=new OnClickListener(){ public void onClick(View arg0) { // throw new UnsupportedOperationException("Not supported yet."); Intent intent=new Intent(); intent.setAction(Intent.ACTION_EDIT); Bundle b=new Bundle(); BroadcastTest.this.sendBroadcast(intent); } }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); params=new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); layout=new LinearLayout(this); layout.setOrientation(LinearLayout.VERTICAL); sendButt=new Button(this); sendButt.setText("Send"); sendButt.setOnClickListener(sendListener); layout.addView(sendButt,params); this.setContentView(layout); } }
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.broadcast_test; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; /** * * @author Administrator */ public class TestReceiver extends BroadcastReceiver{ public TestReceiver(){ Log.i("log_filter","receiver start"); } @Override public void onReceive(Context arg0, Intent arg1) { // throw new UnsupportedOperationException("Not supported yet."); Log.i("log_filter","received broadcast"); } }
Intent action list:
http://developer.android.com/reference/android/content/Intent.html
Usual Actions :
http://blog.csdn.net/iamkila/article/details/8096635
1 Intent.ACTION_MAIN
String: android.intent.action.MAIN
标识Activity为一个程序的开始。比较常用。
Input:nothing
Output:nothing
<activity android:name=".Main" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
2 Intent.Action_CALL
Stirng: android.intent.action.CALL
呼叫指定的电话号码。
Input:电话号码。数据格式为:tel:+phone number
Output:Nothing
Intent intent=new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:1320010001");
startActivity(intent);
3 Intent.Action.DIAL
String: action.intent.action.DIAL
调用拨号面板
Intent intent=new Intent();
intent.setAction(Intent.ACTION_DIAL); //android.intent.action.DIAL
intent.setData(Uri.parse("tel:1320010001");
startActivity(intent);
Input:电话号码。数据格式为:tel:+phone number
Output:Nothing
说明:打开Android的拨号UI。如果没有设置数据,则打开一个空的UI,如果设置数据,action.DIAL则通过调用getData()获取电话号码。
但设置电话号码的数据格式为 tel:+phone number.
4 Intent.Action.ALL_APPS
String: andriod.intent.action.ALL_APPS
列出所有的应用。
Input:Nothing.
Output:Nothing.
5 Intent.ACTION_ANSWER
Stirng:android.intent.action.ANSWER
处理呼入的电话。
Input:Nothing.
Output:Nothing.
6 Intent.ACTION_ATTACH_DATA
String: android.action.ATTCH_DATA
别用于指定一些数据应该附属于一些其他的地方,例如,图片数据应该附属于联系人
Input: Data
Output:nothing
7 Intent.ACTION_BUG_REPORT
String: android.intent.action.BUG_REPORT
显示Dug报告。
Input:nothing
output:nothing
8 Intent.Action_CALL_BUTTON
String: android.action.intent.CALL_BUTTON.
相当于用户按下“拨号”键。经测试显示的是“通话记录”
Input:nothing
Output:nothing
Intent intent = new Intent(Intent.ACTION_CALL_BUTTON);
startActivity(intent);
9 Intent.ACTION_CHOOSER
String: android.intent.action.CHOOSER
显示一个activity选择器,允许用户在进程之前选择他们想要的,与之对应的是Intent.ACTION_GET_CONTENT.
10. Intent.ACTION_GET_CONTENT
String: android.intent.action.GET_CONTENT
允许用户选择特殊种类的数据,并返回(特殊种类的数据:照一张相片或录一段音)
Input: Type
Output:URI
int requestCode = 1001;
Intent intent = new Intent(Intent.ACTION_GET_CONTENT); // "android.intent.action.GET_CONTENT"
intent.setType("image/*"); // 查看类型,如果是其他类型,比如视频则替换成 video/*,或 */*
Intent wrapperIntent = Intent.createChooser(intent, null);
startActivityForResult(wrapperIntent, requestCode);
11 Intent.ACTION_VIEW
String android.intent.action.VIEW
用于显示用户的数据。
比较通用,会根据用户的数据类型打开相应的Activity。
比如 tel:13400010001打开拨号程序,http://www.g.cn则会打开浏览器等。
Uri uri = Uri.parse("http://www.google.com"); //浏览器
Uri uri =Uri.parse("tel:1232333"); //拨号程序
Uri uri=Uri.parse("geo:39.899533,116.036476"); //打开地图定位
Intent it = new Intent(Intent.ACTION_VIEW,uri);
startActivity(it);
//播放视频
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("file:///sdcard/media.mp4");
intent.setDataAndType(uri, "video/*");
startActivity(intent);
//调用发送短信的程序
Intent it = new Intent(Intent.ACTION_VIEW);
it.putExtra("sms_body", "信息内容...");
it.setType("vnd.android-dir/mms-sms");
startActivity(it);
12 Intent.ACTION_SENDTO
String: android.intent.action.SENDTO
说明:发送短信息
//发送短信息
Uri uri = Uri.parse("smsto:13200100001");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
it.putExtra("sms_body", "信息内容...");
startActivity(it);
//发送彩信,设备会提示选择合适的程序发送
Uri uri = Uri.parse("content://media/external/images/media/23");
//设备中的资源(图像或其他资源)
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra("sms_body", "内容");
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setType("image/png");
startActivity(it);
//Email
Intent intent=new Intent(Intent.ACTION_SEND);
String[] tos={"[email protected]"};
String[] ccs={"[email protected]"};
intent.putExtra(Intent.EXTRA_EMAIL, tos);
intent.putExtra(Intent.EXTRA_CC, ccs);
intent.putExtra(Intent.EXTRA_TEXT, "The email body text");
intent.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
intent.setType("message/rfc822");
startActivity(Intent.createChooser(intent, "Choose Email Client"));
13 Intent.ACTION_GET_CONTENT
//选择图片 requestCode 返回的标识
Intent intent = new Intent(Intent.ACTION_GET_CONTENT); //"android.intent.action.GET_CONTENT"
intent.setType(contentType); //查看类型 String IMAGE_UNSPECIFIED = "image/*";
Intent wrapperIntent = Intent.createChooser(intent, null);
((Activity) context).startActivityForResult(wrapperIntent, requestCode);
//添加音频
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(contentType); //String VIDEO_UNSPECIFIED = "video/*";
Intent wrapperIntent = Intent.createChooser(intent, null);
((Activity) context).startActivityForResult(wrapperIntent, requestCode);
//拍摄视频
int durationLimit = getVideoCaptureDurationLimit(); //SystemProperties.getInt("ro.media.enc.lprof.duration", 60);
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
intent.putExtra(MediaStore.EXTRA_SIZE_LIMIT, sizeLimit);
intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, durationLimit);
startActivityForResult(intent, REQUEST_CODE_TAKE_VIDEO);
//视频
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(contentType); //String VIDEO_UNSPECIFIED = "video/*";
Intent wrapperIntent = Intent.createChooser(intent, null);
((Activity) context).startActivityForResult(wrapperIntent, requestCode);
//录音
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(ContentType.AUDIO_AMR); //String AUDIO_AMR = "audio/amr";
intent.setClassName("com.android.soundrecorder",
"com.android.soundrecorder.SoundRecorder");
((Activity) context).startActivityForResult(intent, requestCode);
//拍照 REQUEST_CODE_TAKE_PICTURE 为返回的标识
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //"android.media.action.IMAGE_CAPTURE";
intent.putExtra(MediaStore.EXTRA_OUTPUT, Mms.ScrapSpace.CONTENT_URI); // output,Uri.parse("content://mms/scrapSpace");
startActivityForResult(intent, REQUEST_CODE_TAKE_PICTURE);
Register an BroadcastReceiver inside the program:
1. instance an IntentFilter: IntentFilter filter=new IntentFilter();
2. register the action in intent filter : filter.addAction("android.provider.Telephony.SMS_RECEIVED");
3. instance my Receiver ,which extends BroadcastReceiver :
smsreceiver=new SMSReceiver();
4. register the receiver in the context:
Context.registerReceiver(smsreceiver,filter);
5. unregister: Context.unregisterReceiver(smsreceiver);
6. remember to add the neccessery permision in manifest file
package com.broadcast_test_02; import android.app.Activity; import android.content.IntentFilter; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; public class BroadcastTest02 extends Activity { private static final String SMS_ACTION="android.provider.Telephony.SMS_RECEIVED"; private LinearLayout layout; private Button registerButt; private Button unregisterButt; private LinearLayout.LayoutParams params; private SMSReceiver smsreceiver; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); params=new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); layout=new LinearLayout(this); layout.setOrientation(LinearLayout.VERTICAL); registerButt=new Button(this); registerButt.setText("register broadcast receiver"); registerButt.setOnClickListener(registerListener); layout.addView(registerButt,params); unregisterButt=new Button(this); unregisterButt.setText("unregister broadcast receiver"); unregisterButt.setOnClickListener(unregisterListener); layout.addView(unregisterButt,params); this.setContentView(layout); } private OnClickListener registerListener=new OnClickListener(){ public void onClick(View arg0) { // throw new UnsupportedOperationException("Not supported yet."); smsreceiver=new SMSReceiver(); IntentFilter filter=new IntentFilter(); filter.addAction(SMS_ACTION); BroadcastTest02.this.registerReceiver(smsreceiver, filter); } }; private OnClickListener unregisterListener=new OnClickListener(){ public void onClick(View arg0) { // throw new UnsupportedOperationException("Not supported yet."); BroadcastTest02.this.unregisterReceiver(smsreceiver); Log.i("log_filter", "unregister receiver"); } }; }
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.broadcast_test_02; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.telephony.SmsMessage; import android.util.Log; /** * * @author Administrator */ public class SMSReceiver extends BroadcastReceiver{ public SMSReceiver(){ Log.i("log_filter","create sms receiver"); } @Override public void onReceive(Context arg0, Intent intent) { // throw new UnsupportedOperationException("Not supported yet."); Log.i("log_filter","received sms:"+intent.getExtras().get("pdus")); Bundle bundle=intent.getExtras(); Object[] myObjPdus=(Object[])bundle.get("pdus"); SmsMessage[] messages=new SmsMessage[myObjPdus.length]; for(int i=0;i<myObjPdus.length;i++){ messages[i]=SmsMessage.createFromPdu((byte[])myObjPdus[i]); Log.i("log_filter", "sms:"+messages[i].getDisplayMessageBody()); } } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.broadcast_test_02" android:versionCode="1" android:versionName="1.0"> <application android:label="@string/app_name" > <activity android:name="BroadcastTest02" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.RECEIVE_SMS"/> </manifest>