Android 如何从小部件的按钮调用类

Android 如何从小部件的按钮调用类,android,xml,android-widget,Android,Xml,Android Widget,我的小部件中有两个按钮,我想调用两个不同的类。这是我的代码示例 package com.example.test; import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.Date; import android.annotation.SuppressLint; import android.app.PendingIntent; import android.appwidget.App

我的小部件中有两个按钮,我想调用两个不同的类。这是我的代码示例

    package com.example.test;


import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

import android.annotation.SuppressLint;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;

//import android.text.format.DateFormat;
import android.util.Log;
//import android.view.View;
//import android.view.View.OnClickListener;

//import android.widget.Button;
//import android.widget.LinearLayout;
import android.widget.RemoteViews;

@SuppressLint("SimpleDateFormat")
public class MainActivity extends AppWidgetProvider {
      SimpleDateFormat df = new SimpleDateFormat("hh:mm:ss");

      public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        final int N = appWidgetIds.length;



       // Log.i("ExampleWidget",  "Updating widgets " + Arrays.asList(appWidgetIds));

        // Perform this loop procedure for each App Widget that belongs to this
        // provider
        for (int i = 0; i < N; i++) {
          int appWidgetId = appWidgetIds[i];

          // Create an Intent to launch ExampleActivity
          Intent intent = new Intent(context, SecondClass.class);
          PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 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.activity_main);
          views.setOnClickPendingIntent(R.id.button1, pendingIntent);

          Intent intent1 = new Intent(context, ThirdClass.class);
          PendingIntent pending = PendingIntent.getActivity(context, 0, intent1, 0);

          RemoteViews views1 = new RemoteViews(context.getPackageName(), R.layout.activity_main);
          views1.setOnClickPendingIntent(R.id.button2, pending);  

         appWidgetManager.updateAppWidget(appWidgetId, views);
        } 
      } 
    }
清单文件是

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.test"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.test.ThirdClass"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name="com.example.test.SecondClass"
            android:label="@string/name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name="com.example.test.MainActivity" android:label="8 test">
  <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />

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

</manifest>

当我按下按钮1时,调用SecondClass.class,但当按下按钮2时,不会调用ThirdClass.class。谁能帮帮我吗。Thnaks

您已经创建了第二个名为views1的RemoteViews对象,并为其中一个视图提供了启动第三个类的PendingEvent。但是当您更新appwidget时,您会给出第一个名为views的RemoteView。很明显,这些是不同的,因此在您的小部件中没有挂起的内容可以转到第三类

我想你应该这样做:

int appWidgetId = appWidgetIds[i];

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

// Create an Intent to launch SecondClass 
Intent intent = new Intent(context, SecondClass.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.button1, pendingIntent);

// Create an Intent to launch ThirdClass
Intent intent1 = new Intent(context, ThirdClass.class);
PendingIntent pending = PendingIntent.getActivity(context, 0, intent1, 0);
views.setOnClickPendingIntent(R.id.button2, pending);  

appWidgetManager.updateAppWidget(appWidgetId, views);

为什么要创建视图1?非常感谢。成功了。非常感谢你。