码迷,mamicode.com
首页 > 移动开发 > 详细

android 意图和四大组件

时间:2016-01-13 21:54:28      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:

   
                        意图 Intent
                        
                
    一、显式意图
        直接指定要实现的类:
            Intent intent=new Intent(MainActivity.this,xxx.class);
            this.startActivity(intent);
        
    二、隐式意图
        1:xml配置:在意图所指向的类的配置中添加属性<intent-filter></intent-filter>,如:
            <activity
            android:name="com.example.activity.twoActivity"
            android:label="two"
                <intent-filter>
                    <action android:name="com.xxx.xxxx"/>
                    <data android:mimetype="text/String"/>
                    <category android:name="android.intent.category.DEFAULT"/>
                </intent-filter>
            android:launchMode="singleInstance"/>
        
        2:在窗体activity中使用:
            Intent intent =new Intent();
            intent.setAction("com.xxxx.xxxx");
            intent.setType("text/String");
            intent.addCategory(Intent.CATEGORY.DEFAULT);
            this.startActivity(intent)
    
        注意:在隐式意图中可以同时对应多个动作/类,在启动时会自动给用户选择。
    
    
                        四大组件:Activity、Contentprovider、broadcastreceive、service            
                                        
                    
    一、Activity  窗体
        
        1、生命周期
            1.onCreate() 窗体创建时被调用
            2.onStart()    窗体界面可见时被调用
            3.onResume()    用户得到焦点时被调用
            4.onPause()    失去焦点时被调用
            5.onStop()    窗体界面不可见时被调用
            6.onDestory()    窗体销毁时被调用
            7.onRestart   重新开始窗体
            打开窗体:1-2-3
            最小化: 4-5
            恢复窗体大小:7-2-3
            关闭窗体:4-5-6
            
        2、横竖屏幕切换
            <activity android:configChanges="orentagion|keyboardHidden|screenSize"  --窗口变化不重置
                      android:screenOrentation="landscape"    ---设置窗口方向、默认自动感应。landscape:横向。
        
        3、activity的启动模式
            <activity launchMode=""
            standard:默认模式
            singleTop:如果activity在顶部,系统就不会重新创建.而是调用现在activity的onNewIntent方法.
            singleTask:保持任务栈只有一个activity,如果要启动的activity已经存在,会复用已存在的activity,把activity之上的窗口全部清掉
            singInstance:整个系统只有一个activity存在.它会运行在自已独立任务栈中,并且任务栈只有它一个实例存在
                    
    二、Contentprovider     内容提供者
        
    三、BroadcastReceiver  广播接收者
    
        1、有序广播        ————所有人都可以接受到,不可以修改不可以拦截。
            1.发广播
                public void sendBroadcast(View view){
                    Intent intent =new Intent();
                    intent.setAction("com.xxx.xxxx");
                    this.sendBroadcast(intent);
                }
            
            2.接收广播
                配置文件:
                    <activity>
                    </activity>
                    <receiver android:name="com.xxxx.xxxxx">  --广播名称
                        <intent-filter>
                        <action android:name="xxxxxx"/>  --检测对象名称
                        </intent-filter>
                    </receiver>
                
                广播.java文件:
                    继承 Broadcastreceiver
                
                可以获取对方数据和修改(不是指广播内容)
                    例如:打电话
                    this.getResultData();  获取对方拨出的电话
                    this.setResultData("xxx"); 修改对方拨出的电话
            
        2、无序广播        ——————一级一级的往下传,可以修改可以拦截广播内容。
            1.发有序广播
                Intent intent =new Intent();
                intent.setAction("com.xxx.xxxx");
                this.sendOrderedBroadcast(intent,recriverPermission,resultReceiver,scheduler,initialCode,initialData,initialExtras);
                    如:this.sendOrderedBroadcast(intent,null,null,null,"400","广播内容",null);
                
            2.接收有序广播
                配置文件:
                        <activity>
                        </activity>
                        <receiver android:name="com.xxxx.xxxxx3">  --广播名称
                            <intent-filter android:priority="2000">        ----代表级别,数字大的先接收
                            <action android:name="xxxxxx"/>  --检测对象名称
                            </intent-filter>
                        </receiver>
                        <receiver android:name="com.xxxx.xxxxx2">  --广播名称
                            <intent-filter android:priority="3000">
                            <action android:name="xxxxxx"/>  --检测对象名称
                            </intent-filter>
                        </receiver>
                        
                广播.java文件:
                    继承 Broadcastreceiver    
                    this.getResultData();  获取广播内容
                    this.setResultData("xxx"); 修改广播内容
                    this.abortBroadcast();拦截停止广播
                
                
    四、service  服务     
                    ------一种运行在后台的组件(类)
    
        1、关于进程和进程的优先级别
            进程  ————一个应用程序。
            优先级:由高到低
                1.Foreground process 前台进程  用户正在使用的进程
                2.visible process 可见进程 可以看见的进程  (例如悬浮窗)
                3.service process 服务进程 有后台服务的进程
                4.background process 后台进程 最小化但没有关闭的进程
                5.Empty process 没有任何运行的Activity (返回键一直返回到桌面)
                
        2、服务的生命周期
            onCreate() 创建的时候调用
            onStartCommand() 被启用时调用
            onDestory() 销毁时被调用
            
        3、使用
            1.创建
            新建java  继承 service
            配置文件:
            <service android:name="com.xxx.xxx"></service>
            2.使用
            启用:(如果服务不存在,创建一个服务,再开启)
                Intent intent=new Intent(MainActivity.this,xxxx.cladss);
                this.startService(intent);
            关闭:
                Intent intent=new Intent(MainActivity.this,xxxx.class);
                this.stopService(intent);

android 意图和四大组件

标签:

原文地址:http://www.cnblogs.com/zjw0914/p/5128449.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!