繁体   English   中英

使用带有按钮的小部件在Android上设置设备音量,简单的问题……?

[英]Using a widget with buttons to set device volume on Android, easy question…?

我只是想制作一个带有+按钮,-按钮和进度条的小部件。 该栏应显示当前音量,加号和减号按钮应控制电话音量。 现在,我只是想让加号按钮执行其功能(启动小部件时,进度条已经显示了正确的音量),但是每当我单击按钮时,都会强制关闭。 我之前曾经做过应用程序,但从未做过小部件,并且我没有过多地使用intent。 这是我的Widget唯一的类的代码:

package com.habosa.volumeslider;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.util.Log;
import android.widget.RemoteViews;

public class VolumeWidgetProvider extends AppWidgetProvider {
    private AudioManager volume;
    private int whichStream = AudioManager.STREAM_RING;


public void goRinger() {
    whichStream = AudioManager.STREAM_RING;
}

public void goMedia() {
    whichStream = AudioManager.STREAM_MUSIC;
}
public void goAlarm() {
    whichStream = AudioManager.STREAM_ALARM;
}
public void goNotification() {
    whichStream = AudioManager.STREAM_NOTIFICATION;
}

public void volumeUp() {
    volume.adjustStreamVolume(whichStream, AudioManager.ADJUST_RAISE, 0);
}

public void volumeDown() {
    volume.adjustStreamVolume(whichStream, AudioManager.ADJUST_LOWER, 0);
}

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    volume = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    int maxVol = volume.getStreamMaxVolume(whichStream);
    int currentVol = volume.getStreamVolume(whichStream);
    RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.main);
    remoteView.setProgressBar(R.id.ProgressBar01, maxVol, currentVol, false);

    Intent plus = new Intent(context, VolumeWidgetProvider.class);
    plus.putExtra("msg", "plus");
    plus.setAction("BUTTONPRESS");

    PendingIntent plusPendingIntent = PendingIntent.getBroadcast(context, 0, plus, 0);

    remoteView.setOnClickPendingIntent(R.id.PlusButton, plusPendingIntent);

    for (int appWidgetId : appWidgetIds) {
      appWidgetManager.updateAppWidget(appWidgetId, remoteView);
    }
}

@Override
public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("BUTTONPRESS")) {
            String msg = "null";
            try {
                msg = intent.getStringExtra("msg");
            } catch (NullPointerException e) {
                Log.e("Error", "msg = null");
            }

            if (msg.equals("plus")) {volumeUp();}

            PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0);
        } else {
            // do nothing
        }

        super.onReceive(context, intent);
    }
}

这是我的清单,如果相关的话:

<?xml version="1.0" encoding="utf-8"?>

<receiver android:name="VolumeWidgetProvider" >
<intent-filter>
    <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    <action android:name="com.habosa.volumeslider.VolumeWidgetProvider.BUTTONPRESS"/>
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/volumewidgetproviderinfo" />
</receiver>

</application>
<uses-sdk android:minSdkVersion="7" />

我究竟做错了什么?

看看https://github.com/commonsguy/cw-andtutorials/tree/master/34-AdvAppWidget ,以了解一个不错的示例。

请注意,解决方案可能不会是从Intent调用应用程序小部件提供程序类,因为您显然正在尝试这样做。

①如果本文未解决您的问题,请点击查看与本文相关的问题
②如果本文未解决您的问题,请向程序员专用AI小助手提问
暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM