Android - 播放视频的小工具(onclick麻烦)

时间:2012-12-16 15:39:57

标签: android widget

我正在尝试创建一个简单的小部件,在单击时将从SD卡中播放电影。这看起来很简单,通过以下教程,我提出了以下代码,但似乎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);

    }
}

非常感谢任何帮助。

谢谢, 约什

1 个答案:

答案 0 :(得分:0)

试试此代码

首先创建一个像这样的变量

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

现在在onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)方法内设置待定意图,如此

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_);
}

然后在onReceive(final Context context, Intent main_intent)方法中这样做。

 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);

            }