Kotlin 卸载APP自身

 1 package com.example.batdw01.myapplication
 2 
 3 import android.net.wifi.WifiManager
 4 import android.support.v7.app.AppCompatActivity
 5 import android.os.Bundle
 6 import android.content.Intent
 7 import android.net.Uri
 8 import kotlinx.android.synthetic.main.activity_main.*
 9 
10 
11 class MainActivity : AppCompatActivity() {
12 
13     override fun onCreate(savedInstanceState: Bundle?) {
14         super.onCreate(savedInstanceState)
15         setContentView(R.layout.activity_main)
16 
17         //Kotlin 给按钮添加事件
18         btnUninstall.setOnClickListener { uninstallMe(); }
19     }
20 
21     fun uninstallMe(){
22 
23         //Kotlin调用字符串资源
24         txtInfo.text = getString( R.string.uninstalling );
25 
26         //Kotlin调用系统功能卸载APP自身
27         val packageUri = Uri.parse("package:" + this@MainActivity.packageName)
28         val intent = Intent(Intent.ACTION_DELETE, packageUri)
29         startActivity(intent)
30     }
31 
32 }

需要权限:

    <permission android:name="android.permission.DELETE_PACKAGES" />

 

posted @ 2017-11-14 08:34  乁卬杨  阅读(422)  评论(0编辑  收藏  举报