Android靜默安裝實現方案,仿360手機助手秒裝和智慧安裝功能
之前有很多朋友都問過我,在Android系統中怎樣才能實現靜默安裝呢?所謂的靜默安裝,就是不用彈出系統的安裝介面,在不影響使用者任何操作的情況下不知不覺地將程式裝好。雖說這種方式看上去不打攪使用者,但是卻存在著一個問題,因為Android系統會在安裝介面當中把程式所宣告的許可權展示給使用者看,使用者來評估一下這些許可權然後決定是否要安裝該程式,但如果使用了靜默安裝的方式,也就沒有地方讓使用者看許可權了,相當於使用者被動接受了這些許可權。在Android官方看來,這顯示是一種非常危險的行為,因此靜默安裝這一行為系統是不會開放給開發者的。
但是總是彈出一個安裝對話方塊確實是一種體驗比較差的行為,這一點Google自己也意識到了,因此Android系統對自家的Google Play商店開放了靜默安裝許可權,也就是說所有從Google Play上下載的應用都可以不用彈出安裝對話方塊了。這一點充分說明了擁有許可權的重要性,自家的系統想怎麼改就怎麼改。借鑑Google的做法,很多國內的手機廠商也採用了類似的處理方式,比如說小米手機在小米商店中下載應用也是不需要彈出安裝對話方塊的,因為小米可以在MIUI中對Android系統進行各種定製。因此,如果我們只是做一個普通的應用,其實不太需要考慮靜默安裝這個功能,因為我們只需要將應用上架到相應的商店當中,就會自動擁有靜默安裝的功能。
但是如果我們想要做的也是一個類似於商店的平臺呢?比如說像360手機助手,它廣泛安裝於各種各樣的手機上,但都是作為一個普通的應用存在的,而沒有Google或小米這樣的特殊許可權,那360手機助手應該怎樣做到更好的安裝體驗呢?為此360手機助手提供了兩種方案, 秒裝(需ROOT許可權)和智慧安裝,如下圖示:
因此,今天我們就模仿一下360手機助手的實現方式,來給大家提供一套靜默安裝的解決方案。
一、秒裝
所謂的秒裝其實就是需要ROOT許可權的靜默安裝,其實靜默安裝的原理很簡單,就是呼叫Android系統的pm install命令就可以了,但關鍵的問題就在於,pm命令系統是不授予我們許可權呼叫的,因此只能在擁有ROOT許可權的手機上去申請許可權才行。
下面我們開始動手,新建一個InstallTest專案,然後建立一個SilentInstall類作為靜默安裝功能的實現類,程式碼如下所示:
/** * 靜默安裝的實現類,呼叫install()方法執行具體的靜默安裝邏輯。 * 原文地址:http://blog.csdn.net/guolin_blog/article/details/47803149 * @author guolin * @since 2015/12/7 */ public class SilentInstall { /** * 執行具體的靜默安裝邏輯,需要手機ROOT。 * @param apkPath * 要安裝的apk檔案的路徑 * @return 安裝成功返回true,安裝失敗返回false。 */ public boolean install(String apkPath) { boolean result = false; DataOutputStream dataOutputStream = null; BufferedReader errorStream = null; try { // 申請su許可權 Process process = Runtime.getRuntime().exec("su"); dataOutputStream = new DataOutputStream(process.getOutputStream()); // 執行pm install命令 String command = "pm install -r " + apkPath + "\n"; dataOutputStream.write(command.getBytes(Charset.forName("utf-8"))); dataOutputStream.flush(); dataOutputStream.writeBytes("exit\n"); dataOutputStream.flush(); process.waitFor(); errorStream = new BufferedReader(new InputStreamReader(process.getErrorStream())); String msg = ""; String line; // 讀取命令的執行結果 while ((line = errorStream.readLine()) != null) { msg += line; } Log.d("TAG", "install msg is " + msg); // 如果執行結果中包含Failure字樣就認為是安裝失敗,否則就認為安裝成功 if (!msg.contains("Failure")) { result = true; } } catch (Exception e) { Log.e("TAG", e.getMessage(), e); } finally { try { if (dataOutputStream != null) { dataOutputStream.close(); } if (errorStream != null) { errorStream.close(); } } catch (IOException e) { Log.e("TAG", e.getMessage(), e); } } return result; } }
可以看到,SilentInstall類中只有一個install()方法,所有靜默安裝的邏輯都在這個方法中了,那麼我們具體來看一下這個方法。首先在第21行呼叫了Runtime.getRuntime().exec("su")方法,在這裡先申請ROOT許可權,不然的話後面的操作都將失敗。然後在第24行開始組裝靜默安裝命令,命令的格式就是pm install -r <apk路徑>,-r引數表示如果要安裝的apk已經存在了就覆蓋安裝的意思,apk路徑是作為方法引數傳入的。接下來的幾行就是執行上述命令的過程,注意安裝這個過程是同步的,因此我們在下面呼叫了process.waitFor()方法,即安裝要多久,我們就要在這裡等多久。等待結束之後說明安裝過程結束了,接下來我們要去讀取安裝的結果並進行解析,解析的邏輯也很簡單,如果安裝結果中包含Failure字樣就說明安裝失敗,反之則說明安裝成功。
整個方法還是非常簡單易懂的,下面我們就來搭建呼叫這個方法的環境。修改activity_main.xml中的程式碼,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.installtest.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onChooseApkFile"
android:text="選擇安裝包" />
<TextView
android:id="@+id/apkPathText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onSilentInstall"
android:text="秒裝" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onForwardToAccessibility"
android:text="開啟智慧安裝服務" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onSmartInstall"
android:text="智慧安裝" />
</LinearLayout>
這裡我們先將程式的主介面確定好,主介面上擁有四個按鈕,第一個按鈕用於選擇apk檔案的,第二個按鈕用於開始秒裝,第三個按鈕用於開啟智慧安裝服務,第四個按鈕用於開始智慧安裝,這裡我們暫時只能用到前兩個按鈕。那麼呼叫SilentInstall的install()方法需要傳入apk路
徑,因此我們需要先把檔案選擇器的功能實現好,新建activity_file_explorer.xml和list_item.xml作為檔案選擇器的佈局檔案,程式碼分別如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dp"
android:orientation="horizontal">
<ImageView android:id="@+id/img"
android:layout_width="32dp"
android:layout_margin="4dp"
android:layout_gravity="center_vertical"
android:layout_height="32dp"/>
<TextView android:id="@+id/name"
android:textSize="18sp"
android:textStyle="bold"
android:layout_width="match_parent"
android:gravity="center_vertical"
android:layout_height="50dp"/>
</LinearLayout>
然後新建FileExplorerActivity作為檔案選擇器的Activity,程式碼如下:public class FileExplorerActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
ListView listView;
SimpleAdapter adapter;
String rootPath = Environment.getExternalStorageDirectory().getPath();
String currentPath = rootPath;
List<Map<String, Object>> list = new ArrayList<>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_file_explorer);
listView = (ListView) findViewById(R.id.list_view);
adapter = new SimpleAdapter(this, list, R.layout.list_item,
new String[]{"name", "img"}, new int[]{R.id.name, R.id.img});
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
refreshListItems(currentPath);
}
private void refreshListItems(String path) {
setTitle(path);
File[] files = new File(path).listFiles();
list.clear();
if (files != null) {
for (File file : files) {
Map<String, Object> map = new HashMap<>();
if (file.isDirectory()) {
map.put("img", R.drawable.directory);
} else {
map.put("img", R.drawable.file_doc);
}
map.put("name", file.getName());
map.put("currentPath", file.getPath());
list.add(map);
}
}
adapter.notifyDataSetChanged();
}
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
currentPath = (String) list.get(position).get("currentPath");
File file = new File(currentPath);
if (file.isDirectory())
refreshListItems(currentPath);
else {
Intent intent = new Intent();
intent.putExtra("apk_path", file.getPath());
setResult(RESULT_OK, intent);
finish();
}
}
@Override
public void onBackPressed() {
if (rootPath.equals(currentPath)) {
super.onBackPressed();
} else {
File file = new File(currentPath);
currentPath = file.getParentFile().getPath();
refreshListItems(currentPath);
}
}
}
這部分程式碼由於和我們本篇檔案的主旨沒什麼關係,主要是為了方便demo展示的,因此我就不進行講解了。
接下來修改MainActivity中的程式碼,如下所示:
/**
* 仿360手機助手秒裝和智慧安裝功能的主Activity。
* 原文地址:http://blog.csdn.net/guolin_blog/article/details/47803149
* @author guolin
* @since 2015/12/7
*/
public class MainActivity extends AppCompatActivity {
TextView apkPathText;
String apkPath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
apkPathText = (TextView) findViewById(R.id.apkPathText);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0 && resultCode == RESULT_OK) {
apkPath = data.getStringExtra("apk_path");
apkPathText.setText(apkPath);
}
}
public void onChooseApkFile(View view) {
Intent intent = new Intent(this, FileExplorerActivity.class);
startActivityForResult(intent, 0);
}
public void onSilentInstall(View view) {
if (!isRoot()) {
Toast.makeText(this, "沒有ROOT許可權,不能使用秒裝", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(apkPath)) {
Toast.makeText(this, "請選擇安裝包!", Toast.LENGTH_SHORT).show();
return;
}
final Button button = (Button) view;
button.setText("安裝中");
new Thread(new Runnable() {
@Override
public void run() {
SilentInstall installHelper = new SilentInstall();
final boolean result = installHelper.install(apkPath);
runOnUiThread(new Runnable() {
@Override
public void run() {
if (result) {
Toast.makeText(MainActivity.this, "安裝成功!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "安裝失敗!", Toast.LENGTH_SHORT).show();
}
button.setText("秒裝");
}
});
}
}).start();
}
public void onForwardToAccessibility(View view) {
}
public void onSmartInstall(View view) {
}
/**
* 判斷手機是否擁有Root許可權。
* @return 有root許可權返回true,否則返回false。
*/
public boolean isRoot() {
boolean bool = false;
try {
bool = new File("/system/bin/su").exists() || new File("/system/xbin/su").exists();
} catch (Exception e) {
e.printStackTrace();
}
return bool;
}
}
可以看到,在MainActivity中,我們對四個按鈕點選事件的回撥方法都進行了定義,當點選選擇安裝包按鈕時就會呼叫onChooseApkFile()方法,當點選秒裝按鈕時就會呼叫onSilentInstall()方法。在onChooseApkFile()方法方法中,我們通過Intent打開了FileExplorerActivity,然後在onActivityResult()方法當中讀取選擇的apk檔案路徑。在onSilentInstall()方法當中,先判斷裝置是否ROOT,如果沒有ROOT就直接return,然後判斷安裝包是否已選擇,如果沒有也直接return。接下來我們開啟了一個執行緒來呼叫SilentInstall.install()方法,因為安裝過程會比較耗時,如果不開執行緒的話主執行緒就會被卡住,不管安裝成功還是失敗,最後都會使用Toast來進行提示。
程式碼就這麼多,最後我們來配置一下AndroidManifest.xml檔案:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.installtest">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".FileExplorerActivity"/>
</application>
</manifest>
並沒有什麼特殊的地方,由於選擇apk檔案需要讀取SD卡,因此在AndroidManifest.xml檔案中要記得宣告讀SD卡許可權。
另外還有一點需要注意,在Android 6.0系統中,讀寫SD卡許可權被列為了危險許可權,因此如果將程式的targetSdkVersion指定成了23則需要做專門的6.0適配,這裡簡單起見,我把targetSdkVersion指定成了22,因為6.0的適配工作也不在文章的講解範圍之內。
現在執行程式,就可以來試一試秒裝功能了,切記手機一定要ROOT,效果如下圖所示:
可以看到,這裡我們選擇的網易新聞安裝包已成功安裝到手機上了,並且沒有彈出系統的安裝介面,由此證明秒裝功能已經成功實現了。
二、智慧安裝
那麼對於ROOT過的手機,秒裝功能確實可以避免彈出系統安裝介面,在不影響使用者操作的情況下實現靜默安裝,但是對於絕大部分沒有ROOT的手機,這個功能是不可用的。那麼我們應該怎麼辦呢?為此360手機助手提供了一種折中方案,就是藉助Android提供的無障礙服務來實現智慧安裝。所謂的智慧安裝其實並不是真正意義上的靜默安裝,因為它還是要彈出系統安裝介面的,只不過可以在安裝介面當中釋放使用者的操作,由智慧安裝功能來模擬使用者點選,安裝完成之後自動關閉介面。這個功能是需要使用者手動開啟的,並且只支援Android 4.1之後的手機,如下圖所示:
好的,那麼接下來我們就模仿一下360手機助手,來實現類似的智慧安裝功能。
首先在res/xml目錄下新建一個accessibility_service_config.xml檔案,程式碼如下所示:
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:packageNames="com.android.packageinstaller"
android:description="@string/accessibility_service_description"
android:accessibilityEventTypes="typeAllMask"
android:accessibilityFlags="flagDefault"
android:accessibilityFeedbackType="feedbackGeneric"
android:canRetrieveWindowContent="true"
/>
其中,packageNames指定我們要監聽哪個應用程式下的視窗活動,這裡寫com.android.packageinstaller表示監聽Android系統的安裝介面。description指定在無障礙服務當中顯示給使用者看的說明資訊,上圖中360手機助手的一大段內容就是在這裡指定的。accessibilityEventTypes指定我們在監聽視窗中可以模擬哪些事件,這裡寫typeAllMask表示所有的事件都能模擬。accessibilityFlags可以指定無障礙服務的一些附加引數,這裡我們傳預設值flagDefault就行。accessibilityFeedbackType指定無障礙服務的反饋方式,實際上無障礙服務這個功能是Android提供給一些殘疾人士使用的,比如說盲人不方便使用手機,就可以藉助無障礙服務配合語音反饋來操作手機,而我們其實是不需要反饋的,因此隨便傳一個值就可以,這裡傳入feedbackGeneric。最後canRetrieveWindowContent指定是否允許我們的程式讀取視窗中的節點和內容,必須寫true。
記得在string.xml檔案中寫一下description中指定的內容,如下所示:
<resources>
<string name="app_name">InstallTest</string>
<string name="accessibility_service_description">智慧安裝服務,無需使用者的任何操作就可以自動安裝程式。</string>
</resources>
接下來修改AndroidManifest.xml檔案,在裡面配置無障礙服務:<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.installtest">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
......
<service
android:name=".MyAccessibilityService"
android:label="我的智慧安裝"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
<meta-data
android:name="android.accessibilityservice"
android:resource="@xml/accessibility_service_config" />
</service>
</application>
</manifest>
這部分配置的內容多數是固定的,必須要宣告一個android.permission.BIND_ACCESSIBILITY_SERVICE的許可權,且必須要有一個值為android.accessibilityservice.AccessibilityService的action,然後我們通過<meta-data>將剛才建立的配置檔案指定進去。
接下來就是要去實現智慧安裝功能的具體邏輯了,建立一個MyAccessibilityService類並繼承自AccessibilityService,程式碼如下所示:
/**
* 智慧安裝功能的實現類。
* 原文地址:http://blog.csdn.net/guolin_blog/article/details/47803149
* @author guolin
* @since 2015/12/7
*/
public class MyAccessibilityService extends AccessibilityService {
Map<Integer, Boolean> handledMap = new HashMap<>();
public MyAccessibilityService() {
}
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
AccessibilityNodeInfo nodeInfo = event.getSource();
if (nodeInfo != null) {
int eventType = event.getEventType();
if (eventType== AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED ||
eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
if (handledMap.get(event.getWindowId()) == null) {
boolean handled = iterateNodesAndHandle(nodeInfo);
if (handled) {
handledMap.put(event.getWindowId(), true);
}
}
}
}
}
private boolean iterateNodesAndHandle(AccessibilityNodeInfo nodeInfo) {
if (nodeInfo != null) {
int childCount = nodeInfo.getChildCount();
if ("android.widget.Button".equals(nodeInfo.getClassName())) {
String nodeContent = nodeInfo.getText().toString();
Log.d("TAG", "content is " + nodeContent);
if ("安裝".equals(nodeContent)
|| "完成".equals(nodeContent)
|| "確定".equals(nodeContent)) {
nodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
return true;
}
} else if ("android.widget.ScrollView".equals(nodeInfo.getClassName())) {
nodeInfo.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
}
for (int i = 0; i < childCount; i++) {
AccessibilityNodeInfo childNodeInfo = nodeInfo.getChild(i);
if (iterateNodesAndHandle(childNodeInfo)) {
return true;
}
}
}
return false;
}
@Override
public void onInterrupt() {
}
}
程式碼並不複雜,我們來解析一下。每當視窗有活動時,就會有訊息回撥到onAccessibilityEvent()方法中,因此所有的邏輯都是從這裡開始的。首先我們可以通過傳入的AccessibilityEvent引數來獲取當前事件的型別,事件的種類非常多,但是我們只需要監聽TYPE_WINDOW_CONTENT_CHANGED和TYPE_WINDOW_STATE_CHANGED這兩種事件就可以了,因為在整個安裝過程中,這兩個事件必定有一個會被觸發。當然也有兩個同時都被觸發的可能,那麼為了防止二次處理的情況,這裡我們使用了一個Map來過濾掉重複事件。接下來就是呼叫iterateNodesAndHandle()方法來去解析當前介面的節點了,這裡我們通過遞迴的方式將安裝介面中所有的子節點全部進行遍歷,當發現按鈕節點的時候就進行判斷,按鈕上的文字是不是“安裝”、“完成”、“確定”這幾種型別,如果是的話就模擬一下點選事件,這樣也就相當於幫使用者自動操作了這些按鈕。另外從Android 4.4系統開始,使用者需要將應用申請的所有許可權看完才可以點選安裝,因此如果我們在節點中發現了ScrollView,那就模擬一下滑動事件,將介面滑動到最底部,這樣安裝按鈕就可以點選了。
最後,回到MainActivity中,來增加對智慧安裝功能的呼叫,如下所示:
/**
* 仿360手機助手秒裝和智慧安裝功能的主Activity。
* 原文地址:http://blog.csdn.net/guolin_blog/article/details/47803149
* @author guolin
* @since 2015/12/7
*/
public class MainActivity extends AppCompatActivity {
......
public void onForwardToAccessibility(View view) {
Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
startActivity(intent);
}
public void onSmartInstall(View view) {
if (TextUtils.isEmpty(apkPath)) {
Toast.makeText(this, "請選擇安裝包!", Toast.LENGTH_SHORT).show();
return;
}
Uri uri = Uri.fromFile(new File(apkPath));
Intent localIntent = new Intent(Intent.ACTION_VIEW);
localIntent.setDataAndType(uri, "application/vnd.android.package-archive");
startActivity(localIntent);
}
}
當點選了開啟智慧安裝服務按鈕時,我們通過Intent跳轉到系統的無障礙服務介面,在這裡啟動智慧安裝服務。當點選了智慧安裝按鈕時,我們通過Intent跳轉到系統的安裝介面,之後所有的安裝操作都會自動完成了。
現在可以重新執行一下程式,效果如下圖所示:
可以看到,當開啟網易新聞的安裝介面之後,我們不需要進行任何的手動操作,介面的滑動、安裝按鈕、完成按鈕的點選都是自動完成的,最終會自動回到手機原來的介面狀態,這就是仿照360手機助手實現的智慧安裝功能。
好的,本篇文章的所有內容就到這裡了,雖說不能說完全實現靜默安裝,但是我們已經在許可權允許的範圍內儘可能地去完成了,並且360手機助手也只能實現到這一步而已,那些被產品經理逼著去實現靜默安裝的程式設計師們也有理由交差了吧?
關注我的技術公眾號,每天都有優質技術文章推送。關注我的娛樂公眾號,工作、學習累了的時候放鬆一下自己。
微信掃一掃下方二維碼即可關注: