標籤:
意圖 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 意圖和四大組件