问题描述
开发人员文档似乎让我失望了.我可以不假思索地创建一个静态小部件,我什至可以创建一个像模拟时钟小部件这样会自动更新的小部件,但是,我一生都无法弄清楚如何创建一个对用户点击时做出反应的小部件它.以下是开发人员文档中给出的关于小部件活动应包含的内容的最佳代码示例(唯一的其他提示是 API 演示,它仅创建了一个静态小部件):
The developer documentation has seemed to have failed me here. I can create a static widget without thinking, I can even create a widget like the analogue clock widget that will update itself, however, I can not for the life of me figure out how to create a widget that reacts to when a user clicks on it. Here is the best code sample that the developer documentation gives to what a widget activity should contain (the only other hint being the API demos, which only creates a static widget):
public class ExampleAppWidgetProvider extends AppWidgetProvider {
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
// 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, ExampleActivity.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.appwidget_provider_layout);
views.setOnClickPendingIntent(R.id.button, pendingIntent);
// Tell the AppWidgetManager to perform an update on the current App Widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
}
因此,当点击小部件时,它看起来像是调用了挂起的意图,这是基于意图的(我不太确定意图和挂起的意图之间的区别是什么),并且意图是为了ExampleActivity 类.所以我让我的示例活动类成为一个简单的活动,在创建时会创建一个媒体播放器对象并启动它(它永远不会释放该对象,所以它最终会崩溃,这是它的代码:
So, it looks like pending intent is called when the widget is clicked, which is based off of an intent (I'm not quite sure what the difference between an intent and a pending intent is), and the intent is for the ExampleActivity class. So I made my sample activity class a simple activity that when created, would create a mediaplayer object, and start it (it wouldn't ever release the object, so it would eventually crash, here is it's code:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.sound);
mp.start();
}
但是,当我将小部件添加到主屏幕并单击它时,没有任何播放,实际上,当我将更新计时器设置为几百毫秒(在 appwidget provider xml 文件中)时,没有任何播放.此外,我设置了断点并发现它不仅永远不会到达活动,而且我设置的断点也不会被触发.(我还没有弄清楚为什么会这样),但是,logcat 似乎表明活动类文件正在运行.
However, when I added the widget to the home screen, and clicked on it, nothing played, in fact, nothing played when I set the update timer to just a few hundred milliseconds (in the appwidget provider xml file). Furthermore, I set break points and found out that not only was it never reaching the activity, but no break points I set would ever get triggered. (I still haven't figured out why that is), however, logcat seemed to indicate that the activity class file was being run.
那么,我可以做些什么来让应用小部件响应点击?由于 onClickPendingIntent()
方法是我发现的最接近 onClick
类型方法的方法.
So, is there anything I can do to get an appwidget to respond to a click? As the onClickPendingIntent()
method is the closest I have found to a onClick
type of method.
推荐答案
首先,添加一个带有常量的静态变量.
First, add a static variable with a constant.
public static String YOUR_AWESOME_ACTION = "YourAwesomeAction";
然后你需要在将意图添加到待处理意图之前将动作添加到意图中:
Then you need to add the action to the intent before you add the intent to the pending intent:
Intent intent = new Intent(context, widget.class);
intent.setAction(YOUR_AWESOME_ACTION);
(其中widget.class是你的AppWidgetProvider的类,你当前的类)
(Where widget.class is the class of your AppWidgetProvider, your current class)
然后你需要使用 getBroadcast 创建一个 PendingIntent
You then need to create a PendingIntent with getBroadcast
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
为小部件中的可点击视图设置 onClickPendingIntent
Set the onClickPendingIntent for the clickable view in your widget
remoteView.setOnClickPendingIntent(R.id.widgetFrameLayout, pendingIntent);
接下来,重写同一个类中的onReceive方法:
Next, override the onReceive method in the same class:
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
然后通过在 onReceive 方法中查询为您的操作返回的意图来响应您的按钮按下:
And then respond to your button presses by querying the intent returned for your action within the onReceive method:
if (intent.getAction().equals(YOUR_AWESOME_ACTION)) {
//do some really cool stuff here
}
那就应该做到!
这篇关于android中的可点击小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!