从一个小部件使用preferencesActivity? [英] Using PreferencesActivity from a widget?

查看:203
本文介绍了从一个小部件使用preferencesActivity?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在preferenceActivity似乎是一个非常简单的辅助类来管理应用程序的preferences。

The PreferenceActivity seems like a really easy helper class to manage the preferences of an app.

我想还可以使用从部件相同的类。

I'd like to also use that same class from the widget.

在一个小部件的第一个实例我想要的preferenceActivity上来。这是很容易从配置活动做的,但这里是毛刺...

On the first instance of a widget I want the PreferenceActivity to come up. This was very easy to do from the configuration activity but here is the glitch...

我不能告诉时preference编辑完成!

I can't tell when the preference edit is completed!

小部件preferences我在网络上使用手动内置preference屏幕,而不是preferenceActivity辅助类,然后侦听被点击保存按钮,看到的例子。

All examples of widget preferences I see on the net use a manually built preference screen rather than the PreferenceActivity helper class and then listen for the 'save' button being clicked.

我怎么能做到这一点的preferenceActivity因为该活动没有保存按钮。你只需要使用返回按钮,提交您的preferences变化。

How can I do that with the PreferenceActivity since that activity doesn't have a save button. You just use the 'back' button to commit your preferences changes.

谢谢!

推荐答案

我一直在试图做同样的事情,我想我已经解决了。我处理onBack pressed()事件在preferenceActivity,并从那里用执行窗口更新sendBroadcast()。

I have been trying to do the same thing and I think I've cracked it. I handle the onBackPressed() event in the PreferenceActivity and perform a widget update from there using sendBroadcast().

在preferenceActivity:

In PreferenceActivity:

@Override
public void onBackPressed() {
    Intent intent=getIntent();
    Bundle extras=intent.getExtras();   
    int widgetId=extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);

    // this is the intent broadcast/returned to the widget
    Intent updateIntent = new Intent(this, WidgetProvider.class);
    updateIntent.setAction("PreferencesUpdated");
    updateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
    sendBroadcast(updateIntent);
}

在您WidgetProvider:

In your WidgetProvider:

@Override
public void onReceive(Context context, Intent intent) {
    if ("PreferencesUpdated".equals(action)) {
        // update your widget here
        // my widget supports multiple instances so I needed to uniquely identify them like this
        RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.widget);
        int appWidgetId = intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
        updateWidgetView(context, remoteView, appWidgetId);
    }   
}

注:在1​​.5 onBack pressed不支持所以onBack pressed注释掉@覆盖并添加此code

NOTE: In 1.5 onBackPressed isn't support so comment out the @Override for onBackPressed and add this code

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    Log.d(LOG_TAG, "onKeyDown() "+ keyCode);
    if (keyCode==KeyEvent.KEYCODE_BACK && Integer.parseInt(Build.VERSION.SDK)<5) {
        onBackPressed();
    }
}

我要补充一点,我是新来Android开发,所以我可能会做这完全是错误的。我只能说,这是为我工作:)

I should add that I'm new to Android development so I may be doing this entirely wrong. All I can say it is works for me :)

让我知道你上车。

这篇关于从一个小部件使用preferencesActivity?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆