gpt4 book ai didi

java - 在 Android 上使用带有按钮的小部件来设置设备音量,这是一个简单的问题...?

转载 作者:行者123 更新时间:2023-12-01 15:52:17 25 4
gpt4 key购买 nike

我只是想制作一个带有+按钮、-按钮和进度条的小部件。该栏应显示当前音量,加号和减号按钮应控制手机音量。现在,我只是想让加号按钮执行其功能(启动小部件时进度条已经显示正确的音量),但每当我单击该按钮时,我都会强制关闭。我以前做过应用程序,但从未做过小部件,而且我也没有太多地使用 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);
}
}

这是我的 list (如果相关):

<?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 调用应用程序小部件提供程序类,正如您当前显然正在尝试的那样。

关于java - 在 Android 上使用带有按钮的小部件来设置设备音量,这是一个简单的问题...?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5782589/

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