gpt4 book ai didi

android - 长按应用程序图标

转载 作者:行者123 更新时间:2023-11-29 16:06:09 26 4
gpt4 key购买 nike

对于我正在构建的应用程序,我希望用户能够在启动应用程序时长按图标。这将为用户调出一个包含不同选项的菜单。这可能吗?在讨论菜单时,我查看了开发人员页面并看到了 ActionMode.Callback 接口(interface)。这是否适合应用程序图标而不是应用程序本身?

谢谢。

我试图让它工作,但我无处可去,所以我决定尝试从上下文菜单中访问不同的应用程序。 context_home 案例不起作用。能否请你帮忙。

public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info;

try {

info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();

} catch (ClassCastException e) {

Log.e(TAG, "bad menuInfo", e);

return false;
}

Uri noteUri = ContentUris.withAppendedId(getIntent().getData(), info.id);

switch (item.getItemId()) {

case R.id.context_open:

startActivity(new Intent(Intent.ACTION_EDIT, noteUri));

return true;

case R.id.context_copy:

ClipboardManager clipboard = (ClipboardManager)

getSystemService(Context.CLIPBOARD_SERVICE);

clipboard.setPrimaryClip(ClipData.newUri(getContentResolver(),"Note",noteUri));

return true;

case R.id.context_delete:

getContentResolver().delete(noteUri,null, null);

return true;

case R.id.context_home:

PackageManager pm = getPackageManager();
Intent intent = pm.getLaunchIntentForPackage("project,android.project");
startActivity(intent);
return true;

default:
return super.onContextItemSelected(item);
}
}

最佳答案

使用App Shortcuts (从 API 级别 25 开始)。

  • AndroidManifest.xml 中找到您的主要 Activity
  • 使用定义的快捷方式添加对资源的引用:

    <meta-data android:name="android.app.shortcuts"
    android:resource="@xml/shortcuts" />
  • 在引用文件中定义快捷方式:

    <shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
    android:shortcutId="compose"
    android:enabled="true"
    android:icon="@drawable/compose_icon"
    android:shortcutShortLabel="@string/compose_shortcut_short_label1"
    android:shortcutLongLabel="@string/compose_shortcut_long_label1"
    android:shortcutDisabledMessage="@string/compose_disabled_message1">
    <intent
    android:action="android.intent.action.VIEW"
    android:targetPackage="com.example.myapplication"
    android:targetClass="com.example.myapplication.ComposeActivity" />
    <!-- If your shortcut is associated with multiple intents, include them
    here. The last intent in the list determines what the user sees when
    they launch this shortcut. -->
    <categories android:name="android.shortcut.conversation" />
    </shortcut>
    <!-- Specify more shortcuts here. -->
    </shortcuts>

关于android - 长按应用程序图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18183292/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com