锁定屏幕小部件按钮不起作用

时间:2015-01-02 05:28:44

标签: java android

我在获取一个非常简单的Android小部件按钮时遇到了一些麻烦,该按钮启动了在锁屏上工作的活动。我可以在主屏幕上确认小部件的工作原理。我需要做什么让按钮工作?

WidgetProvider.Java

    public class WidgetProvider  extends AppWidgetProvider  {


    public static String WIDGET_BUTTON = "com.test.WIDGET_BUTTON";
    public static String WIDGET_STOP_BUTTON = "com.test.WIDGET_STOP_BUTTON";
    MediaPlayer WidgetMP;

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {

        ComponentName watchWidget = new ComponentName(context, WidgetProvider.class);
        appWidgetManager.updateAppWidget(watchWidget, createWidget(context));


    }

    @Override

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

        //Toast.makeText(context, "onRecieve", Toast.LENGTH_SHORT).show();

        if (WIDGET_BUTTON.equals(intent.getAction())) {

            Toast.makeText(context, "Start Button", Toast.LENGTH_SHORT).show();

            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

            RemoteViews remoteViews;
            ComponentName watchWidget;

            remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
            watchWidget = new ComponentName(context, WidgetProvider.class);

            Intent newIntent = new Intent(context, Alarm.class);   
            newIntent.putExtra("widget", true);
            newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            context.startActivity(newIntent);

            appWidgetManager.updateAppWidget(watchWidget, remoteViews);
            }

          if (WIDGET_STOP_BUTTON.equals(intent.getAction())) {

            Toast.makeText(context, "Stop Button", Toast.LENGTH_SHORT).show();

            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

            RemoteViews remoteViews;
            ComponentName watchWidget;

            remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
            watchWidget = new ComponentName(context, WidgetProvider.class);

            Intent newIntent = new Intent(context, Test.class);   
            newIntent.putExtra("widget", false);
            newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            context.startActivity(newIntent);

            appWidgetManager.updateAppWidget(watchWidget, remoteViews);
          }

       }

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




 public void onAppWidgetOptionsChanged(Context context, AppWidgetManager manager,
                                          int appWidgetId, Bundle widgetOptions) {

        manager.updateAppWidget(appWidgetId, createWidget(context));
 }


 private RemoteViews createWidget(Context context) {
     RemoteViews remoteViews;
     ComponentName watchWidget;

     remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);

     remoteViews.setOnClickPendingIntent(R.id.widget_alarm_button, getPendingSelfIntent(context, WIDGET_BUTTON));
     remoteViews.setOnClickPendingIntent(R.id.widget_stop_button, getPendingSelfIntent(context, WIDGET_STOP_BUTTON));
     return remoteViews;
 }

}

widget_provider.xml

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:initialKeyguardLayout="@layout/widget"
    android:initialLayout="@layout/widget"
    android:minHeight="146dp" 
    android:minWidth="146dp"
    android:updatePeriodMillis="0"
    android:widgetCategory="home_screen|keyguard"
    >
</appwidget-provider>

0 个答案:

没有答案