Android:Intent

AndroidManifest.xml中intent-filter

<activity android:name=".Opening_Animation.Opening_Animation"
             android:theme="@style/opening_screen">
    <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
    </intent-filter>
</activity>

在intent-fileter区域像上面这样加上android.intent.action.MAIN,android.intent.category.LAUNCHER各自都有什么作用?

1.要弄清楚这个问题,首先需要弄明白什么是implicit(隐藏) intent什么是explicit(明确) intent。
Explicit Intent明确的指定了要启动的Acitivity ,比如以下Java代码:

Intent intent= new Intent(this, B.class) 

而Implicit Intent没有明确的指定要启动哪个Activity ,而是通过设置一些Intent Filter来让系统去筛选合适的Acitivity去启动。

2.intent到底发给哪个activity,需要进行三个匹配,一个是action,一个是category,一个是data。
理论上来说,如果intent不指定category,那么无论intent filter的内容是什么都应该是匹配的。但是,如果是implicit intent,android默认给加上一个CATEGORY_DEFAULT,这样的话如果intent filter中没有android.intent.category.DEFAULT这个category的话,匹配测试就会失败。
所以,如果你的 activity支持接收implicit intent的话就一定要在intent filter中加入android.intent.category.DEFAULT。
例外情况是:android.intent.category.MAIN和android.intent.category.LAUNCHER的filter中没有必要加入android.intent.category.DEFAULT,当然加入也没有问题。
我们定义的activity如果接受implicit intent的话,intent filer就一定要加上android.intent.category.DEFAULT这个category。

3.在intent-fileter区域像上面这样加上android.intent.action.MAIN,android.intent.category.LAUNCHER各自都有什么作用?下面来实际解释这个问题。
一个应用程序可以有多个Activity,每个Activity是同级别的,那么在启动程序时,需要指定先启动哪个Activity。
有些程序可能需要显示在程序列表里,有些不需要。怎么定义呢?
android.intent.action.MAIN决定应用程序最先启动的Activity , android.intent.category.LAUNCHER决定应用程序是否显示在程序列表里。因为你的程序可能有很多个activity只要xml配置文件中有这么一个intent-filter,而且里面有这个launcher,那么这个activity就是点击程序时最先运行的那个activity。
android.intent.category.LAUNCHER也是用于模拟器设置默认打开的activity的,如果不加这个,模拟器启动时会让你选择要先启动的activity。

使用Intent显示调用一个activity并获取返回值

startActivity()函数可以从一个Activity启动另外一个Activity,但不能获取其返回值。所以想要获取返回值,就要使用startActivityForResult()函数。这里利用Intent的putExtra等函数接口,传递和接收数据。
下面来看一下例子:
有两个A,B Activity。A启动B,B执行完之后返回值给A,并重新启动A。
A actvity:

//-- A.java --//
//启动B
Intent bintent = new Intent(A.this, B.class);
//设置 bintent的Bundle的一个值
String bsay = "Hello, this is B speaking"; 
bintent.putExtra("listenB", bsay)
startActivityForResult(bintent,0); 
// 参数(Intent intent, Int requestCode) 的 requestCode 对应下面回收Bundle时识别用的

//重写onActivityResult()来处理返回的数据,建议先看B.java 的代码再回来看这里比较好理解
//这理有三个参数 requestCode, resultCode, data
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (resultCode) { //resultCode为回传的标记,我在B中回传的是RESULT_OK
        case RESULT_OK:
            Bundle b=data.getExtras();  //data为B中回传的Intent
            String str=b.getString("ListenB");//str即为回传的值"Hello, this is B speaking"
            /* 得到B回传的数据后做什么... 略 */
            break;
        default:
            break;
    }
}

B activity:

//-- B.java --//
// 用 setResut() 准备好要回传的数据后,只要使用finish()的方法就能把打包好的数据发给A且运行onActivityResult()部分的代码

Intent aintent = getIntent();//拿来一个Intent
String a = aintent.getExtra("listenB");
System.out.println(a);//打印A activity传过来的字符串
/* 将数据打包到aintent Bundle 的过程略 */
setResut(RESULT_OK,aintent); //这理有2个参数(int resultCode, Intent intent)
... ...
finish();

流程如下:
这里写图片描述

startActivityForResult(Intent intent, Int requestCode)
intent 传给B的。requestCode >=0就好,随便用于在onActivityResult()区别哪个子模块回传的数据,如果还有C.java ,D甚至E子模块的话,每个区分开不同的requestCode就好。

setResut(int resultCode, Intent intent)
resultCode 如果B子模块可能有几种不同的结果返回,可以用这个参数予以识别区分。这里还有个特殊的 RESULT_OK 值,没有特殊情况用它就好了,sdk有说明。intent 继续不解释,传回给A的onActivityResult()。

onActivityResult(int requestCode, int resultCode, Intent intent)
这里三个都不用解释了,与上面对应的东西。如果不对requestCode和resultCode 加以识别区分的话,只要有其他activity setResult到了A onActivityResult()会无差别处理。

setResut()函数之后的finish()函数只是让当前Activity移出栈,并不会释放其资源。关于onDestroy(),finish(),System.exit(0)等区别如下:

  • Activity.finish()
    Call this when your activity is done and should be closed.
    在你的activity动作完成的时候,或者Activity需要关闭的时候,调用此方法。
    当你调用此方法的时候,系统只是将最上面的Activity移出了栈,并没有及时的调用onDestory()方法,其占用的资源也没有被及时释放。因为移出了栈,所以当你点击手机上面的“back”按键的时候,也不会再找到这个Activity。

  • Activity.onDestory()
    the system is temporarily destroying this instance of the activity to save space.
    系统暂时销毁了这个Activity的实例在内存中占据的空间。
    在Activity的生命周期中,onDestory()方法是他生命的最后一步,资源空间等就被回收了。当重新进入此Activity的时候,必须重新创建,执行onCreate()方法。

  • System.exit(0)
    这玩意是退出整个应用程序的,是针对整个Application的。将整个进程直接KO掉。

顺便看看Activity的生命周期
这里写图片描述

Intent Action的用法

1.Intent.ACTION_MAIN
标识Activity为一个程序的开始。

<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
呼叫指定的电话号码

Intent intent=new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:1320010001");
startActivity(intent);

3.ntent.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
列出所有的应用。

5.Intent.ACTION_ANSWER
处理呼入的电话

6.Intent.ACTION_ATTACH_DATA
用于指定一些数据应该附属于一些其他的地方,例如,图片数据应该附属于联系人

7.Intent.ACTION_BUG_REPORT
显示Dug报告

8.Intent.Action_CALL_BUTTON
相当于用户按下“拨号”键。经测试显示的是“通话记录”

Intent intent = new Intent(Intent.ACTION_CALL_BUTTON);
startActivity(intent);

9.Intent.ACTION_CHOOSER
显示一个activity选择器,允许用户在进程之前选择他们想要的,与之对应的是Intent.ACTION_GET_CONTENT.

10.Intent.ACTION_GET_CONTENT
允许用户选择特殊种类的数据,并返回(特殊种类的数据:照一张相片或录一段音)

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);  
//从gallery选取图片 
Intent i = new Intent(); 
i.setType("image/*"); 
i.setAction(Intent.ACTION_GET_CONTENT); 
startActivityForResult(i, 11); 

11.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);
//显示地图 
Uri uri = Uri.parse("geo:38.899533,-77.036476"); 
Intent it = new Intent(Intent.Action_VIEW,uri); 
startActivity(it); 
//路径规划 
Uri uri = Uri.parse("http://maps.google.com/maps?f=dsaddr=startLat%20startLng&daddr=endLat%20endLng&hl=en"); 
Intent it = new Intent(Intent.ACTION_VIEW,URI); 
startActivity(it); 
//调用发短信的程序 
Intent it = new Intent(Intent.ACTION_VIEW);    
it.putExtra("sms_body", "The SMS text");    
it.setType("vnd.android-dir/mms-sms");    
startActivity(it); 

12.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={"android1@163.com"}; 
String[] ccs={"you@yahoo.com"}; 
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);  

14.从google搜索内容

Intent intent = new Intent(); 
intent.setAction(Intent.ACTION_WEB_SEARCH); 
intent.putExtra(SearchManager.QUERY,"searchString") 
startActivity(intent); 

15.install apk

Uri installUri = Uri.fromParts("package", "xxx", null); 
returnIt = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri); 

16.uninstall apk

Uri uri = Uri.fromParts("package", strPackageName, null);    
Intent it = new Intent(Intent.ACTION_DELETE, uri);    
startActivity(it); 

17.打开录音机

Intent mi = new Intent(Media.RECORD_SOUND_ACTION); 
startActivity(mi); 

18.显示应用详细列表

Uri uri = Uri.parse("market://details?id=app_id");         
Intent it = new Intent(Intent.ACTION_VIEW, uri);         
startActivity(it);         
//where app_id is the application ID, find the ID          
//by clicking on your application on Market home          
//page, and notice the ID from the address bar      

刚才找app id未果,结果发现用package name也可以 
Uri uri = Uri.parse("market://details?id=<packagename>"); 
这个简单多了 

19.寻找应用

Uri uri = Uri.parse("market://search?q=pname:pkg_name");         
Intent it = new Intent(Intent.ACTION_VIEW, uri);         
startActivity(it); 
//where pkg_name is the full package path for an application

20.打开联系人列表

<1>            
Intent i = new Intent(); 
i.setAction(Intent.ACTION_GET_CONTENT); 
i.setType("vnd.android.cursor.item/phone"); 
startActivityForResult(i, REQUEST_TEXT); 

<2> 
Uri uri = Uri.parse("content://contacts/people"); 
Intent it = new Intent(Intent.ACTION_PICK, uri); 
startActivityForResult(it, REQUEST_TEXT); 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值