Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 實戰教學第六篇之一鍵鎖屏應用問題解決

編輯:關於 Android 程式設計

上一篇初步開發了這個應用,功能都有了(見前文〈一鍵鎖屏應用〉)。

但遺留了兩個問題:1、還是無法卸載;2、必須手動去點擊應用程式進入程式,再點擊按鈕,這顯得很麻煩。

這一篇就來解決上面兩個問題,做出最好的效果。

首先解決無法卸載的問題:

在清單檔案中,再配置一個一鍵卸載的主活動(兩個主活動無所謂,只不過在桌面上顯示兩個圖示而已,其實仍然是一個應用)。

 <activity android:name="com.itydl.lockscreen.Remove" android:label="一鍵卸載" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> 

在新建的活動裡面,寫取消激活和卸載的程式碼:

 package com.itydl.lockscreen; import android.app.Activity; import android.app.admin.DevicePolicyManager; import android.content.ComponentName; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; public class Remove extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // super.onCreate(savedInstanceState); setContentView(R.layout.activity_remove); } public void remove(View v){ //調用卸載的界面,要去上層源碼中看一下卸載界面的意圖是什麼。通過查看源碼,如下: /** * <intent-filter> <action android:name="android.intent.action.VIEW" /> <action android:name="android.intent.action.DELETE" /> <category android:name="android.intent.category.DEFAULT" /> <data android:scheme="package" /> </intent-filter> */ //在卸載之前首先要取消設激活設備管理器 //獲取設備管理器 DevicePolicyManager dpm = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE); ComponentName who = new ComponentName(this,DeviceAdminSample.class); dpm.removeActiveAdmin(who);//Remove a current administration component. This can only be called by the application that owns the administration component //卸載,啟動卸載界面 Intent remove = new Intent("android.intent.action.DELETE"); remove.addCategory("android.intent.category.DEFAULT"); remove.setData(Uri.parse("package:"+getPackageName()));//表示卸載本程序,Return the name of this application's package. startActivity(remove);//卸載用戶apk界面 } } 

但要注意一點,新建的一鍵卸載佈局,一定要做以下修改:

 <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > --------------------這裡一定修改。 <Button android:onClick="remove" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="一鍵卸載" /> </RelativeLayout> 

運行程式,當使用者想要卸載時,點擊一鍵卸載進入卸載介面,即可完成對這個應用的卸載。

最後在使用者體驗上做點文章:

這個時候,一鍵鎖屏是透過點擊按鈕實現的,為了省事,不要開啟鎖屏的介面。把鎖屏介面按鈕全部刪掉,背景設置為透明色。

 <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00000000"> ---------------------設置為透明色 </RelativeLayout> 

在鎖屏活動的 onCreate 方法裡面加入一句程式碼:lockScreen(null),表示不需要任何 view 去啟動點擊事件。這樣打開應用的時候就調用 lockScreen(View v) 裡面的鎖屏或者開啟設備管理器功能,程式碼如下:

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); dmp = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE); lockScreen(null); } 

同時,一鍵卸載的應用也做以上同樣的事情。

重新運行程式,一運行就進入激活設備管理器介面。現在把一鍵鎖屏圖示放置到主介面,一點擊就鎖屏,特別方便。而且想卸載這個程式的時候,一點擊就直接卸載掉了。

到現在為止,這個應用算是完美開發完畢了。

下面是應用的截圖:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved