[英]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調用應用程序小部件提供程序類,因為您顯然正在嘗試這樣做。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.