Android小部件-单击imageView打开应用程序

Android小部件-单击imageView打开应用程序,android,widget,clickable,Android,Widget,Clickable,我有一个小部件,当您单击imageView资源时,它会打开一个web地址。我真的很难适应代码。我希望打开指定的应用程序,而不是web地址 package com.example.widget; import android.net.Uri; import android.app.PendingIntent; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; import

我有一个小部件,当您单击imageView资源时,它会打开一个web地址。我真的很难适应代码。我希望打开指定的应用程序,而不是web地址

package com.example.widget;

import android.net.Uri;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
import android.widget.Toast;

public class MainActivity extends AppWidgetProvider{

   @Override
   public void onUpdate(Context context, AppWidgetManager appWidgetManager,
   int[] appWidgetIds) {
      for(int i=0; i<appWidgetIds.length; i++){
      int currentWidgetId = appWidgetIds[i];
      String url = "http://www.website.com";
      Intent intent = new Intent(Intent.ACTION_VIEW);
      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      intent.setData(Uri.parse(url));
      PendingIntent pending = PendingIntent.getActivity(context, 0,
      intent, 0);
      RemoteViews views = new RemoteViews(context.getPackageName(),
      R.layout.activity_main);
      views.setOnClickPendingIntent(R.id.imageView2, pending);
      appWidgetManager.updateAppWidget(currentWidgetId,views);
      Toast.makeText(context, "widget added", Toast.LENGTH_SHORT).show();   
      }
   }    
}
package com.example.widget;
导入android.net.Uri;
导入android.app.pendingent;
导入android.appwidget.AppWidgetManager;
导入android.appwidget.AppWidgetProvider;
导入android.content.Context;
导入android.content.Intent;
导入android.widget.remoteview;
导入android.widget.Toast;
公共类MainActivity扩展了AppWidgetProvider{
@凌驾
public void onUpdate(上下文上下文,AppWidgetManager AppWidgetManager,
int[]appWidgetIds){

对于
Intent Intent=newintent(Intent.ACTION\u视图);
使用所需特定应用程序的包名。
如本示例所示