Android 在主屏幕上创建快捷方式
本例演示了如何以编程方式卸载 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 添加以下代码
package com.example.myapplication; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.net.Uri; import android.os.Bundle; import android.support.v4.content.pm.ShortcutInfoCompat; import android.support.v4.content.pm.ShortcutManagerCompat; import android.support.v4.graphics.drawable.IconCompat; import android.view.View; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private static final String PREF_KEY_SHORTCUT_ADDED = "Sairam"; 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) { if (ShortcutManagerCompat.isRequestPinShortcutSupported(getApplicationContext())) { ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(getApplicationContext(), "#1") .setIntent(new Intent(getApplicationContext(), MainActivity.class).setAction(Intent.ACTION_MAIN)) // !!! intent's action must be set on oreo .setShortLabel("Test") .setIcon(IconCompat.createWithResource(getApplicationContext(), R.drawable.logo)) .build(); ShortcutManagerCompat.requestPinShortcut(getApplicationContext(), shortcutInfo, null); } else { Toast.makeText(MainActivity.this,"launcher does not support short cut icon",Toast.LENGTH_LONG).show(); } } }); } }
步骤 4 − 向 Manifest.xml 添加以下代码
<?xml version = "1.0" encoding = "utf-8"?> <manifest xmlns:android = "http://schemas.android.com/apk/res/android" package = "com.example.myapplication"> <uses-permission android:name = "android.permission.INTERNET" /> <uses-permission android:name = "com.android.launcher.permission.INSTALL_SHORTCUT" /> <application android:allowBackup = "true" android:icon = "@mipmap/ic_launcher" android:label = "@string/app_name" android:roundIcon = "@mipmap/ic_launcher_round" android:supportsRtl = "true" android:theme = "@style/AppTheme"> <activity android:name = ".MainActivity" android:configChanges = "keyboardHidden|orientation|screenSize"> <intent-filter> <action android:name = "android.intent.action.MAIN" /> <action android:name = "android.intent.action.CREATE_SHORTCUT" /> <category android:name = "android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
我们尝试运行应用程序。我假设您已将真实的安卓移动设备与您的计算机连接。要从安卓工作室运行应用程序,请打开您的一个项目活动文件并单击工具栏中的运行 图标。选择您的移动设备作为选项,然后检查您的移动设备,它将显示您的默认屏幕 –
现在单击主页按钮,它将按如下方式显示结果
广告