欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android靜默安裝實現(xiàn)方案 仿360手機(jī)助手秒裝和智能安裝功能

 更新時間:2016年11月15日 10:15:45   作者:guolin  
這篇文章主要介紹了Android靜默安裝實現(xiàn)方案,仿360手機(jī)助手秒裝和智能安裝功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下

之前有很多朋友都問過我,在Android系統(tǒng)中怎樣才能實現(xiàn)靜默安裝呢?所謂的靜默安裝,就是不用彈出系統(tǒng)的安裝界面,在不影響用戶任何操作的情況下不知不覺地將程序裝好。雖說這種方式看上去不打攪用戶,但是卻存在著一個問題,因為Android系統(tǒng)會在安裝界面當(dāng)中把程序所聲明的權(quán)限展示給用戶看,用戶來評估一下這些權(quán)限然后決定是否要安裝該程序,但如果使用了靜默安裝的方式,也就沒有地方讓用戶看權(quán)限了,相當(dāng)于用戶被動接受了這些權(quán)限。在Android官方看來,這顯示是一種非常危險的行為,因此靜默安裝這一行為系統(tǒng)是不會開放給開發(fā)者的。

但是總是彈出一個安裝對話框確實是一種體驗比較差的行為,這一點Google自己也意識到了,因此Android系統(tǒng)對自家的Google Play商店開放了靜默安裝權(quán)限,也就是說所有從Google Play上下載的應(yīng)用都可以不用彈出安裝對話框了。這一點充分說明了擁有權(quán)限的重要性,自家的系統(tǒng)想怎么改就怎么改。借鑒Google的做法,很多國內(nèi)的手機(jī)廠商也采用了類似的處理方式,比如說小米手機(jī)在小米商店中下載應(yīng)用也是不需要彈出安裝對話框的,因為小米可以在MIUI中對Android系統(tǒng)進(jìn)行各種定制。因此,如果我們只是做一個普通的應(yīng)用,其實不太需要考慮靜默安裝這個功能,因為我們只需要將應(yīng)用上架到相應(yīng)的商店當(dāng)中,就會自動擁有靜默安裝的功能。

但是如果我們想要做的也是一個類似于商店的平臺呢?比如說像360手機(jī)助手,它廣泛安裝于各種各樣的手機(jī)上,但都是作為一個普通的應(yīng)用存在的,而沒有Google或小米這樣的特殊權(quán)限,那360手機(jī)助手應(yīng)該怎樣做到更好的安裝體驗?zāi)??為?60手機(jī)助手提供了兩種方案, 秒裝(需ROOT權(quán)限)和智能安裝,如下圖示:

因此,今天我們就模仿一下360手機(jī)助手的實現(xiàn)方式,來給大家提供一套靜默安裝的解決方案。

一、秒裝

所謂的秒裝其實就是需要ROOT權(quán)限的靜默安裝,其實靜默安裝的原理很簡單,就是調(diào)用Android系統(tǒng)的pm install命令就可以了,但關(guān)鍵的問題就在于,pm命令系統(tǒng)是不授予我們權(quán)限調(diào)用的,因此只能在擁有ROOT權(quán)限的手機(jī)上去申請權(quán)限才行。

下面我們開始動手,新建一個InstallTest項目,然后創(chuàng)建一個SilentInstall類作為靜默安裝功能的實現(xiàn)類,代碼如下所示:

/** 
 * 靜默安裝的實現(xiàn)類,調(diào)用install()方法執(zhí)行具體的靜默安裝邏輯。 
 * 原文地址:http://blog.csdn.net/guolin_blog/article/details/47803149 
 * @author guolin 
 * @since 2015/12/7 
 */ 
public class SilentInstall { 
 
 /** 
 * 執(zhí)行具體的靜默安裝邏輯,需要手機(jī)ROOT。 
 * @param apkPath 
 *  要安裝的apk文件的路徑 
 * @return 安裝成功返回true,安裝失敗返回false。 
 */ 
 public boolean install(String apkPath) { 
 boolean result = false; 
 DataOutputStream dataOutputStream = null; 
 BufferedReader errorStream = null; 
 try { 
  // 申請su權(quán)限 
  Process process = Runtime.getRuntime().exec("su"); 
  dataOutputStream = new DataOutputStream(process.getOutputStream()); 
  // 執(zhí)行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; 
  // 讀取命令的執(zhí)行結(jié)果 
  while ((line = errorStream.readLine()) != null) { 
  msg += line; 
  } 
  Log.d("TAG", "install msg is " + msg); 
  // 如果執(zhí)行結(jié)果中包含F(xiàn)ailure字樣就認(rèn)為是安裝失敗,否則就認(rèn)為安裝成功 
  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行調(diào)用了Runtime.getRuntime().exec("su")方法,在這里先申請ROOT權(quán)限,不然的話后面的操作都將失敗。然后在第24行開始組裝靜默安裝命令,命令的格式就是pm install -r <apk路徑>,-r參數(shù)表示如果要安裝的apk已經(jīng)存在了就覆蓋安裝的意思,apk路徑是作為方法參數(shù)傳入的。接下來的幾行就是執(zhí)行上述命令的過程,注意安裝這個過程是同步的,因此我們在下面調(diào)用了process.waitFor()方法,即安裝要多久,我們就要在這里等多久。等待結(jié)束之后說明安裝過程結(jié)束了,接下來我們要去讀取安裝的結(jié)果并進(jìn)行解析,解析的邏輯也很簡單,如果安裝結(jié)果中包含F(xiàn)ailure字樣就說明安裝失敗,反之則說明安裝成功。

整個方法還是非常簡單易懂的,下面我們就來搭建調(diào)用這個方法的環(huán)境。修改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="開啟智能安裝服務(wù)" /> 
 
 <Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:onClick="onSmartInstall" 
 android:text="智能安裝" /> 
</LinearLayout> 

這里我們先將程序的主界面確定好,主界面上擁有四個按鈕,第一個按鈕用于選擇apk文件的,第二個按鈕用于開始秒裝,第三個按鈕用于開啟智能安裝服務(wù),第四個按鈕用于開始智能安裝,這里我們暫時只能用到前兩個按鈕。那么調(diào)用SilentInstall的install()方法需要傳入apk路
徑,因此我們需要先把文件選擇器的功能實現(xiàn)好,新建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); 
 } 
 } 
} 

這部分代碼由于和我們本篇文件的主旨沒什么關(guān)系,主要是為了方便demo展示的,因此我就不進(jìn)行講解了。

接下來修改MainActivity中的代碼,如下所示:

/** 
 * 仿360手機(jī)助手秒裝和智能安裝功能的主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權(quán)限,不能使用秒裝", 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) { 
 
 } 
 
 /** 
 * 判斷手機(jī)是否擁有Root權(quán)限。 
 * @return 有root權(quán)限返回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中,我們對四個按鈕點擊事件的回調(diào)方法都進(jìn)行了定義,當(dāng)點擊選擇安裝包按鈕時就會調(diào)用onChooseApkFile()方法,當(dāng)點擊秒裝按鈕時就會調(diào)用onSilentInstall()方法。在onChooseApkFile()方法方法中,我們通過Intent打開了FileExplorerActivity,然后在onActivityResult()方法當(dāng)中讀取選擇的apk文件路徑。在onSilentInstall()方法當(dāng)中,先判斷設(shè)備是否ROOT,如果沒有ROOT就直接return,然后判斷安裝包是否已選擇,如果沒有也直接return。接下來我們開啟了一個線程來調(diào)用SilentInstall.install()方法,因為安裝過程會比較耗時,如果不開線程的話主線程就會被卡住,不管安裝成功還是失敗,最后都會使用Toast來進(jìn)行提示。

代碼就這么多,最后我們來配置一下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卡權(quán)限。

另外還有一點需要注意,在Android 6.0系統(tǒng)中,讀寫SD卡權(quán)限被列為了危險權(quán)限,因此如果將程序的targetSdkVersion指定成了23則需要做專門的6.0適配,這里簡單起見,我把targetSdkVersion指定成了22,因為6.0的適配工作也不在文章的講解范圍之內(nèi)。

現(xiàn)在運行程序,就可以來試一試秒裝功能了,切記手機(jī)一定要ROOT,效果如下圖所示:

可以看到,這里我們選擇的網(wǎng)易新聞安裝包已成功安裝到手機(jī)上了,并且沒有彈出系統(tǒng)的安裝界面,由此證明秒裝功能已經(jīng)成功實現(xiàn)了。

二、智能安裝

那么對于ROOT過的手機(jī),秒裝功能確實可以避免彈出系統(tǒng)安裝界面,在不影響用戶操作的情況下實現(xiàn)靜默安裝,但是對于絕大部分沒有ROOT的手機(jī),這個功能是不可用的。那么我們應(yīng)該怎么辦呢?為此360手機(jī)助手提供了一種折中方案,就是借助Android提供的無障礙服務(wù)來實現(xiàn)智能安裝。所謂的智能安裝其實并不是真正意義上的靜默安裝,因為它還是要彈出系統(tǒng)安裝界面的,只不過可以在安裝界面當(dāng)中釋放用戶的操作,由智能安裝功能來模擬用戶點擊,安裝完成之后自動關(guān)閉界面。這個功能是需要用戶手動開啟的,并且只支持Android 4.1之后的手機(jī),如下圖所示:

好的,那么接下來我們就模仿一下360手機(jī)助手,來實現(xiàn)類似的智能安裝功能。

智能安裝功能的實現(xiàn)原理要借助Android提供的無障礙服務(wù),關(guān)于無障礙服務(wù)的詳細(xì)講解可參考官方文檔:http://developer.android.com/guide/topics/ui/accessibility/services.html。

首先在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指定我們要監(jiān)聽哪個應(yīng)用程序下的窗口活動,這里寫com.android.packageinstaller表示監(jiān)聽Android系統(tǒng)的安裝界面。description指定在無障礙服務(wù)當(dāng)中顯示給用戶看的說明信息,上圖中360手機(jī)助手的一大段內(nèi)容就是在這里指定的。accessibilityEventTypes指定我們在監(jiān)聽窗口中可以模擬哪些事件,這里寫typeAllMask表示所有的事件都能模擬。accessibilityFlags可以指定無障礙服務(wù)的一些附加參數(shù),這里我們傳默認(rèn)值flagDefault就行。accessibilityFeedbackType指定無障礙服務(wù)的反饋方式,實際上無障礙服務(wù)這個功能是Android提供給一些殘疾人士使用的,比如說盲人不方便使用手機(jī),就可以借助無障礙服務(wù)配合語音反饋來操作手機(jī),而我們其實是不需要反饋的,因此隨便傳一個值就可以,這里傳入feedbackGeneric。最后canRetrieveWindowContent指定是否允許我們的程序讀取窗口中的節(jié)點和內(nèi)容,必須寫true。

記得在string.xml文件中寫一下description中指定的內(nèi)容,如下所示:

<resources> 
 <string name="app_name">InstallTest</string> 
 <string name="accessibility_service_description">智能安裝服務(wù),無需用戶的任何操作就可以自動安裝程序。</string> 
</resources>

 接下來修改AndroidManifest.xml文件,在里面配置無障礙服務(wù):

<?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> 

這部分配置的內(nèi)容多數(shù)是固定的,必須要聲明一個android.permission.BIND_ACCESSIBILITY_SERVICE的權(quán)限,且必須要有一個值為android.accessibilityservice.AccessibilityService的action,然后我們通過<meta-data>將剛才創(chuàng)建的配置文件指定進(jìn)去。

接下來就是要去實現(xiàn)智能安裝功能的具體邏輯了,創(chuàng)建一個MyAccessibilityService類并繼承自AccessibilityService,代碼如下所示:

/** 
 * 智能安裝功能的實現(xiàn)類。 
 * 原文地址: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() { 
 } 
 
} 

代碼并不復(fù)雜,我們來解析一下。每當(dāng)窗口有活動時,就會有消息回調(diào)到onAccessibilityEvent()方法中,因此所有的邏輯都是從這里開始的。首先我們可以通過傳入的AccessibilityEvent參數(shù)來獲取當(dāng)前事件的類型,事件的種類非常多,但是我們只需要監(jiān)聽TYPE_WINDOW_CONTENT_CHANGED和TYPE_WINDOW_STATE_CHANGED這兩種事件就可以了,因為在整個安裝過程中,這兩個事件必定有一個會被觸發(fā)。當(dāng)然也有兩個同時都被觸發(fā)的可能,那么為了防止二次處理的情況,這里我們使用了一個Map來過濾掉重復(fù)事件。

接下來就是調(diào)用iterateNodesAndHandle()方法來去解析當(dāng)前界面的節(jié)點了,這里我們通過遞歸的方式將安裝界面中所有的子節(jié)點全部進(jìn)行遍歷,當(dāng)發(fā)現(xiàn)按鈕節(jié)點的時候就進(jìn)行判斷,按鈕上的文字是不是“安裝”、“完成”、“確定”這幾種類型,如果是的話就模擬一下點擊事件,這樣也就相當(dāng)于幫用戶自動操作了這些按鈕。另外從Android 4.4系統(tǒng)開始,用戶需要將應(yīng)用申請的所有權(quán)限看完才可以點擊安裝,因此如果我們在節(jié)點中發(fā)現(xiàn)了ScrollView,那就模擬一下滑動事件,將界面滑動到最底部,這樣安裝按鈕就可以點擊了。

最后,回到MainActivity中,來增加對智能安裝功能的調(diào)用,如下所示:

/** 
 * 仿360手機(jī)助手秒裝和智能安裝功能的主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); 
 } 
 
} 

當(dāng)點擊了開啟智能安裝服務(wù)按鈕時,我們通過Intent跳轉(zhuǎn)到系統(tǒng)的無障礙服務(wù)界面,在這里啟動智能安裝服務(wù)。當(dāng)點擊了智能安裝按鈕時,我們通過Intent跳轉(zhuǎn)到系統(tǒng)的安裝界面,之后所有的安裝操作都會自動完成了。

現(xiàn)在可以重新運行一下程序,效果如下圖所示:

可以看到,當(dāng)打開網(wǎng)易新聞的安裝界面之后,我們不需要進(jìn)行任何的手動操作,界面的滑動、安裝按鈕、完成按鈕的點擊都是自動完成的,最終會自動回到手機(jī)原來的界面狀態(tài),這就是仿照360手機(jī)助手實現(xiàn)的智能安裝功能。

好的,本篇文章的所有內(nèi)容就到這里了,雖說不能說完全實現(xiàn)靜默安裝,但是我們已經(jīng)在權(quán)限允許的范圍內(nèi)盡可能地去完成了,并且360手機(jī)助手也只能實現(xiàn)到這一步而已,那些被產(chǎn)品經(jīng)理逼著去實現(xiàn)靜默安裝的程序員們也有理由交差了吧?

源碼下載:http://xiazai.jb51.net/201611/yuanma/AndroidInstallTest(jb51.net).rar

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論