1

我正在尝试制作一个简单的小部件,单击该小部件将播放来自 sdcard 的电影。这看起来很简单,通过遵循教程,我提出了以下代码,但似乎永远不会设置 onclick。

显现:

<receiver android:name="WidgetProvider" android:label="DVD Cover">          
        <intent-filter>
            <action 
                android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
        </intent-filter>
            <meta-data 
                android:name="android.appwidget.provider" 
                android:resource="@xml/appwidget_info_2x4"/>
        </receiver>

布局(widget.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/holder"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff777777"
>
<ImageView
android:id="@+id/cover"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#000000"

/>
</LinearLayout>

appwidget.xml:

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


<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
   android:minWidth="200dip"
   android:minHeight="300dip"
   android:updatePeriodMillis="180000"
   android:initialLayout="@layout/widget"
   >
</appwidget-provider>

WidgetProvider.java:

public class WidgetProvider extends AppWidgetProvider {

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


            String movieurl = Environment.getExternalStorageDirectory().getPath() + "/Movie.mp4";


           Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
            notificationIntent.setDataAndType(Uri.parse(movieurl), "video/*");
            PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent,0);


            // Get the layout for the App Widget and attach an on-click listener
            // to the button
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
            views.setOnClickPendingIntent(R.id.holder, contentIntent);


            // Tell the AppWidgetManager to perform an update on the current app widget
            appWidgetManager.updateAppWidget(appWidgetIds, views);

    }
}

任何帮助将不胜感激。

谢谢,乔什

4

1 回答 1

0

Try this code

First create a variable something like this

public static String CLICK_ACTION = "com.widget.CLICK";

Now inside onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) method set pending intent like this

for (int j = 0; j < appWidgetIds.length; j++) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),  R.layout.widget);
Intent clickIntent = new Intent(context, WidgetProvider.class);

    clickIntent.setAction(CLICK_ACTION);
                clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds[j]);
                PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                remoteViews.setOnClickPendingIntent(R.id.cover, pendingIntent_);
}

Then in onReceive(final Context context, Intent main_intent) method do like this.

 if (main_intent.getAction().equals(CLICK_ACTION)) {
         //Start your video intent here
         Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
        notificationIntent.setDataAndType(Uri.parse(movieurl), "video/*");
        context.getApplicationContext().startActivity(notificationIntent);

            }
于 2012-12-16T16:47:14.453 回答