以编程方式卸载 APK
本例演示了如何以编程方式卸载 APK
步骤 1 − 在 Android Studio 中创建新项目,转到文件 ⇒ 新建项目,填写所有必需信息,以创建新项目。
步骤 2 − 将以下代码添加到 res/layout/activity_main.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:gravity = "center" android:layout_height = "match_parent"> <TextView android:id = "@+id/text" android:layout_width = "fill_parent" android:gravity = "center" android:textSize = "30sp" android:layout_height = "wrap_content" android:text = "Click here" /> </LinearLayout>
在上述代码中,我们采用了文本视图。
步骤 3 − 将以下代码添加到 src/MainActivity.java
<?xml version = "1.0" encoding = "utf-8"?>
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
TextView text;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = findViewById(R.id.text);
text.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_DELETE);
intent.setData(Uri.parse("package:com.example.myapplication"));
startActivity(intent);
}
});
}
}让我们运行你的应用程序。我假设你已将真实的 Android 移动设备连接到了电脑。要从 android studio 运行应用,打开项目的某个活动文件,然后点击工具栏中的 运行
图标。选择移动设备,然后检查显示默认界面的移动设备 -

现在,点击主页按钮,将显示以下结果

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP