机器人 - 多appWidgets播放不同的声音 [英] Android - Multiple appWidgets playing different sounds

查看:95
本文介绍了机器人 - 多appWidgets播放不同的声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个Android的音板至极允许用户创建多个桌面小部件,每一个声音。我使用的是活动用户选择他想创建的小部件至极的声音。对于每个插件创建我存储的共享preference在
键=&GT的形式; WIDGET_FILENAME _+为widgetid,值=>文件名
要播放声音,我没有覆盖的widgetProvider类 onRecieve 方法。当桌面小窗口被点击时,它触发一个广播该方法,至极获取从意图的插件ID,然后加载与小部件相关联的共享preference:

I'm writing an android soundboard wich allow the user to create multiple desktop widgets, one for each sound. I'm using an activity for the user to choose wich sound he wants to create the widget for. For each widget created i store a shared preference in the form of
key => "WIDGET_FILENAME_"+widgetId, value=> fileName
To play the sounds, i did override the onRecieve method on the widgetProvider class. When the desktop widget is clicked, it triggers a broadcast to this method, wich gets the widget id from the intent and then loads the shared preference associated with the widget:



int appWidgetId =  intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                                     AppWidgetManager.INVALID_APPWIDGET_ID);


的问题是:该为widgetid始终是相同的,没有马瑟至极插件被点击,使相同的声音被播放
。 在这个任何想法或指导?

The problem is: The widgetId is always the same, no mather wich widget is clicked, causing the same sound to be played.
Any idea or guidance on this?

推荐答案

我有同样的问题,解决它是这样的:

I had the same problem and solved it like this:

  1. 在你AppWidgetProviderClass,声明你的意图如下:

  1. In your AppWidgetProviderClass, declare your Intent as follows:

Intent intent = new Intent(context, YourActivity.class)
        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        .putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);

  • ...和PendingIntent

  • ...and PendingIntent

    PendingIntent pi = PendingIntent.getActivity(context, appWidgetId, intent,
                                                 PendingIntent.FLAG_UPDATE_CURRENT);
    

  • 在活动类,得到a​​ppWidgetId后要更新:

  • In the Activity class, after getting the appWidgetId you want to update:

    int currentWidgetId = this.getIntent().getIntExtra(
           AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
    

  • ...您可以使用类似的功能这一项:

  • ...you can use a function similar to this one:

    private void updateWidgetView() {
        views = new RemoteViews(YourWidget.class.getPackage().getName(),
                                R.layout.main_widget);
        mgr = AppWidgetManager.getInstance(this);
        views.setTextViewText(R.id.some_text_view, someText);
        // Tell the AppWidgetManager to perform an update on the current App Widget
        mgr.updateAppWidget(currentWidgetId, views);
    }
    

  • 这篇关于机器人 - 多appWidgets播放不同的声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆