啓動一個內置程序需要以下步驟:
- 創建intent對象
- 設置action屬性 => 這個action要看啓動哪個內置應用,然後去查對用的字符串或者常量值
- 設置Data屬性
- 啓動
- 部分內置應用想要打開需要權限
權限問題在Android6.0以前直接配置權限即可,之後只能在程序中動態申請獲取用戶的授權
例子1:啓動撥號,打電話界面
/*************** 撥號界面 *******************/
Intent intent = new Intent();
// 這個字符串就是撥號界面的action值
intent.setAction("android.intent.action.DIAL");
// 這個界面不需要什麼權限
// * 是電話號碼,設置data屬性可以直接將電話號輸入完
intent.setData(Uri.parse("tel:***********"));
startActivity(intent);
*************** 打電話的界面|呼叫 *****************/
intent.setAction(Intent.ACTION_CALL); // 常量方式
// 這個需要權限,如果不動態申請,可以配置完,在手機設置裏面授予權限
<!-- 寫在application的外面 -->
<uses-permission android:name="android.permission.CALL_PHONE"/>
例子2:發送短息程序
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SENDTO);
// 這個是要直接到給10086發送信息
intent.setData(Uri.parse("smsto:10086"));
// 這個是消息體,直接準備出要發送的內容
intent.putExtra("sms_body","這是短信的內容");
startActivity(intent);
例子3:打開內置地圖
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse("geo:39.89,116.3"));
startActivity(intent);
例子4:打開內置瀏覽器
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.baidu.com"));
startActivity(intent);
可以看出,操作方式一樣
PendingIntent對象
Intent的意思是意圖,可以理解爲想要到的目的地
PendingIntent的意思是潛在意圖,是未來的時候啓動
比如說:
消息通知,如果我們按以前的博客寫法,就只是一個通知,大部分的通知點一下都會啓動這個應用程序,PendingIntent即可實現。
下面我們直接找出可以實現點擊啓動程序的不同點
NotificationManager manager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
Notification.Builder builder = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("channel_id",
"通知渠道的名稱",NotificationManager.IMPORTANCE_DEFAULT);
manager.createNotificationChannel(channel);
builder = new Notification.Builder(MainActivity.this,"channel_id");
}else {
builder = new Notification.Builder(MainActivity.this);
}
builder.setSmallIcon(R.drawable.ball);
builder.setContentTitle("標題");
builder.setContentText("內容");
//期望將來用戶點擊該通知時,能夠啓動當前APP,就需要使用PendingIntent
/****************************************************************************************************/
// 只有這裏的代碼不同!!!
// pendingIntent生成的最重要的兩個參數是intent和context對象,此處的context對象沒有了解過,但是應該和上一篇一樣,也是目的組件的環境上下文
// 首先這個intent對象要設置目的組件
Intent intent = new Intent(MainActivity.this,
MainActivity.class);
// 聲明PendingIntent有三種方法,分別對應三大組件
// 下面參數的意義:
// 1. 目的環境上下文
// 2. 請求碼
// 3. intent對象
// 4. flags對象 => 對活動棧的處理,這個例子的意思就是如果當前打開着這個頁面,刷新一下即可,不重新建
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,
100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// 在通知構造器上設置上潛在意圖
builder.setContentIntent(pendingIntent);//設置潛在意圖
/*************************************************************************************************************/
Notification notification = builder.build();
manager.notify(100,notification);
PendingIntent的三種構造方式:
- getActivity(Context, int, Intent, int) 跳轉到一個activity組件
- getBroadcast(Context, int, Intent, int) 發送一個廣播組件
- getService(Context, int, Intent, int) 啓動一個服務組件
參數意義一樣,都是context上下文,請求碼,intent對象,flags