启动快捷方式时android发送广播

时间:2018-03-21 23:08:19

标签: android

我只想在启动快捷方式时发送广播 搜索后,我没有找到任何解决方案,我发现一个想法,它启动一个没有显示的活动,并从它发送广播,我这样做就像这样

 <activity android:name=".activity.shortcut"
        android:exported="true"
        android:excludeFromRecents="true"
        android:theme="@style/AppTheme.Transparent"
        ></activity>

和主题Transparent

   <style name="AppTheme.Transparent" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:backgroundDimEnabled">false</item>
</style>

和安装快捷方式的代码

 Intent shortcutIntent = new Intent(getContext(), shortcut.class);


        shortcutIntent.putExtra("name","name");
        shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        //shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); //no effect
        //shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); //no effect

        Intent addIntent = new Intent();
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, conversationList.getFname());


        addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, mBitmap);

        addIntent.putExtra("duplicate", false);
        addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        getContext().sendBroadcast(addIntent);

运行应用程序并创建快捷方式 当我点击快捷方式时,它打开所有应用程序并发送brodcast 所以我只是想在应用程序中没有启动主要活动的情况下打开活动 或者只是在点击快捷方式时发送广播

1 个答案:

答案 0 :(得分:0)

使用其他活动的快捷方式,例如

 <activity android:name="send">

您的应用应该有一个名为send的活动,其中包含d onResume()

发送广播的呼叫代码。

或者使用动态意图并发送一些快捷方式的数据部分,如

 setShortLabel("send-broadcasts") 

https://developer.android.com/guide/topics/ui/shortcuts.html#dynamic

相关问题