android - 安装任何新应用程序后切换小部件停止工作

标签 android android-widget

我正在设计一个可以使媒体音量静音和取消静音的切换小部件。在我安装任何新应用程序之前,它一直很好用……然后它就停止工作了。没有强制关闭或任何东西,当我按下它时它什么也没做。 (如果我删除小部件并再次添加它,它将再次开始工作。)使用 log cat 我看不到任何与我的应用程序相关的消息。

我已经在下面发布了代码,但是如果您能从概念上告诉我当我安装一个新应用程序时主屏幕会发生什么,以及基本上我可以在哪里进行故障排除,那也会有所帮助。

感谢您的帮助。我已经在这里待了好几天了,就是想不通。我有现货 android 4.04 Galaxy Nexus。

public class JCrashWidget extends AppWidgetProvider {
public static String ACTION_WIDGET_RECEIVER = "com.JCrash.widget.ACTION_WIDGET_RECEIVER";
public static String ACTION_WIDGET_CONFIGURE = "com.JCrash.widget.ACTION_WIDGET_CONFIGURE";
private RemoteViews remoteViews = new RemoteViews("com.JCrash", R.layout.widgetstyle1 );

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){
    super.onUpdate(context, appWidgetManager, appWidgetIds);    

    Intent mediaClick = new Intent(context, JCrashWidget.class);
    mediaClick.setAction(ACTION_WIDGET_RECEIVER);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(context,  0, mediaClick, 0);       
    remoteViews.setOnClickPendingIntent(R.id.headphonesid, pendingIntent);

    appWidgetManager.updateAppWidget(appWidgetIds,remoteViews );
}

public void onReceive(Context context, Intent intent)
{
    super.onReceive(context, intent);

    AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    ComponentName cn = new ComponentName(context, JCrashWidget.class);

    int cVol = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    SharedPreferences myPrefs = context.getSharedPreferences("myPrefs", Context.MODE_WORLD_WRITEABLE);
    SharedPreferences.Editor prefsEditor = myPrefs.edit();

        if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) {
            if (cVol == 0)
            {
                cVol = myPrefs.getInt("PREVIOUS_VOLUME", 0);
                    remoteViews.setImageViewResource(R.id.headphonesid,R.drawable.headphones);
                    audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, cVol,
                    AudioManager.FLAG_SHOW_UI);
                    cVol = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
            }

            else 
            {
                prefsEditor.putInt("PREVIOUS_VOLUME", cVol);
                prefsEditor.commit();
                cVol = myPrefs.getInt("PREVIOUS_VOLUME", 0);

                remoteViews.setImageViewResource(R.id.headphonesid,R.drawable.headphones_off);
                audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, AudioManager.FLAG_SHOW_UI);

                cVol = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
            }
            AppWidgetManager.getInstance(context).updateAppWidget(cn, remoteViews);             
        }

}

最佳答案

想通了!我必须在 OnReceive 方法和 OnUpdate 方法中再次设置 OnClickPendingIntent。我将只创建一个函数来为小部件设置未决 Intent ,并从 OnReceive 和 OnUpdate 调用它。

关于android - 安装任何新应用程序后切换小部件停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10972677/

相关文章:

java - 安卓异常: ConcurentModificationException

android - 显示对话框代码未执行

android - 将 RemoteView 转换为 View

android - 使用 LinearLayout 的自定义小部件没有得到 onDraw()

android - 如何将主要 Activity 和日期选择器分离到自己的类(class)

android - 资源安卓 :attr/android:progressBarStyle not found

java - 每当我的 Android AlertDialog 加载、应用程序崩溃时,如何修复错误

java - 从 Android 应用程序中的第二个线程更新 View

Android:字符串可见性问题

android - 我们可以提供到单个图像的不同部分的导航吗