【发布时间】:2014-06-16 23:53:37
【问题描述】:
我的小部件中有两个按钮,我想调用两个不同的类。这是我的代码示例
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>
当我按下 button1 时,会调用“SecondClass.class”,但按下 button2 时不会调用“ThirdClass.class”。有人能帮帮我吗。谢谢
【问题讨论】:
-
你为什么要创建
views1? -
非常感谢。有效。非常感谢。
标签: android xml android-widget