如何从小部件的按钮调用类
Posted
技术标签:
【中文标题】如何从小部件的按钮调用类【英文标题】:How to call classes from a button from a widget 【发布时间】: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
?
非常感谢。有效。非常感谢。
【参考方案1】:
您已经创建了第二个 RemoteViews 对象(称为views1
),并为它的一个视图提供了启动 ThirdClass 的 PendingIntent。但是当你更新 appwidget 时,你会给出第一个 RemoteViews(称为views
)。显然这些不一样,因此您的小部件中没有 PendingIntent 可以转到 ThirdClass。
我想你想这样做:
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);
【讨论】:
以上是关于如何从小部件的按钮调用类的主要内容,如果未能解决你的问题,请参考以下文章
如何从小部件的函数返回值,并将其传递给 Tkinter,Python 中的另一个小部件的函数