国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

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

2019-12-12 04:39:26
字體:
供稿:網(wǎng)友

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

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

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

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

一、秒裝

所謂的秒裝其實就是需要ROOT權(quán)限的靜默安裝,其實靜默安裝的原理很簡單,就是調(diào)用Android系統(tǒng)的pm install命令就可以了,但關鍵的問題就在于,pm命令系統(tǒng)是不授予我們權(quán)限調(diào)用的,因此只能在擁有ROOT權(quán)限的手機上去申請權(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í)行具體的靜默安裝邏輯,需要手機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字樣就認為是安裝失敗,否則就認為安裝成功   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é)果并進行解析,解析的邏輯也很簡單,如果安裝結(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="開啟智能安裝服務" />   <Button  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:onClick="onSmartInstall"  android:text="智能安裝" /> </LinearLayout> 

這里我們先將程序的主界面確定好,主界面上擁有四個按鈕,第一個按鈕用于選擇apk文件的,第二個按鈕用于開始秒裝,第三個按鈕用于開啟智能安裝服務,第四個按鈕用于開始智能安裝,這里我們暫時只能用到前兩個按鈕。那么調(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);  }  } } 

這部分代碼由于和我們本篇文件的主旨沒什么關系,主要是為了方便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權(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) {   }   /**  * 判斷手機是否擁有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)方法都進行了定義,當點擊選擇安裝包按鈕時就會調(diào)用onChooseApkFile()方法,當點擊秒裝按鈕時就會調(diào)用onSilentInstall()方法。在onChooseApkFile()方法方法中,我們通過Intent打開了FileExplorerActivity,然后在onActivityResult()方法當中讀取選擇的apk文件路徑。在onSilentInstall()方法當中,先判斷設備是否ROOT,如果沒有ROOT就直接return,然后判斷安裝包是否已選擇,如果沒有也直接return。接下來我們開啟了一個線程來調(diào)用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卡權(quán)限。

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

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

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

二、智能安裝

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

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

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

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

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

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

接下來就是要去實現(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() {  }  } 

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

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

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

/**  * 仿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跳轉(zhuǎn)到系統(tǒng)的無障礙服務界面,在這里啟動智能安裝服務。當點擊了智能安裝按鈕時,我們通過Intent跳轉(zhuǎn)到系統(tǒng)的安裝界面,之后所有的安裝操作都會自動完成了。

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

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

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

源碼下載:http://xiazai.VeVB.COm/201611/yuanma/AndroidInstallTest(VeVB.COm).rar

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 凉山| 兴仁县| 赫章县| 军事| 石景山区| 侯马市| 牡丹江市| 双流县| 阜阳市| 荥经县| 桃园市| 花莲市| 扬中市| 巴彦淖尔市| 中卫市| 张掖市| 新疆| 万盛区| 十堰市| 霍林郭勒市| 清水县| 桦川县| 吴忠市| 射阳县| 富源县| 康保县| 海宁市| 丹东市| 望都县| 合水县| 博白县| 从江县| 文成县| 乌拉特中旗| 石狮市| 龙陵县| 柳江县| 龙岩市| 伊川县| 吴忠市| 邯郸市|