通过应用小部件管理点击按钮

时间:2018-07-31 10:25:09

标签: android android-studio widget android-widget

我正在使用仅用于窗口小部件的应用程序,以在单击窗口小部件时播放警报声音。我只想在单击小部件时开始播放声音,如果再次单击则停止播放。我面临两个问题,发现很难解决这个问题。 1)当我将小部件拖放到主屏幕时,警报开始响起。 2)单击小部件时,声音无法停止。

帮助将不胜感激... 这是我的密码

androidManifest.xml

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <receiver android:name=".BuzzerWidget">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>

        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/buzzer_widget_info" />
    </receiver>
</application>

appwidget_Provider.xml

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialKeyguardLayout="@layout/buzzer_widget"
android:initialLayout="@layout/buzzer_widget"
android:minHeight="40dp"
android:minWidth="40dp"
android:previewImage="@drawable/buzzer_image"
android:updatePeriodMillis="10000"
android:widgetCategory="home_screen"></appwidget-provider>

小部件布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#09C"
android:padding="@dimen/widget_margin">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/buzzer_layout"
    android:orientation="vertical">

    <ImageButton
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/buzzerButton"
        android:src="@drawable/buzzer_image"/>  
</LinearLayout>

小部件Java文件

public class BuzzerWidget extends AppWidgetProvider {
MediaPlayer mplayer;
private static boolean isPlaying = false;

void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                            int appWidgetId) {
    Log.d("BUZZER", "on updateAppWidget");
    // Construct the RemoteViews object
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.buzzer_widget);

    Intent intent = new Intent(context, BuzzerWidget.class);
    intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
    int[] idArray = new int[]{appWidgetId};
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, idArray);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, appWidgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    remoteViews.setOnClickPendingIntent(R.id.buzzerButton, pendingIntent);
    mplayer = MediaPlayer.create(context, R.raw.buzzer_alarm);

    if (isPlaying) {
        isPlaying = false;

        Log.d("BUZZER", "playing");
        if(mplayer != null) {
            mplayer.release();
            mplayer = null;
        }

    } else {
        isPlaying = true;

        Log.d("BUZZER", "not playing");

        if (!mplayer.isPlaying()){
            mplayer.start();
        }

    }

    // Instruct the widget manager to update the widget
    appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    // There may be multiple widgets active, so update all of them
    for (int appWidgetId : appWidgetIds) {
        Log.d("BUZZER", "on update");
        updateAppWidget(context, appWidgetManager, appWidgetId);
    }
}

protected PendingIntent getPendingSelfIntent(Context context, String action) {
    Intent intent = new Intent(context, getClass());
    intent.setAction(action);
    return PendingIntent.getBroadcast(context, 0, intent, 0);
}

}

0 个答案:

没有答案