【问题标题】:How to keep widget configuration after phone restart?手机重启后如何保留小部件配置?
【发布时间】:2013-07-31 09:45:16
【问题描述】:

我想制作一个能够拨打号码的小部件应用程序,用户可以在他第一次使用小部件配置将小部件拖放到主屏幕时设置号码。但是当手机重新启动小部件时,它会再次使用默认号码。我决定将输入的电话号码保存到 Shared Preferences 以保存和加载用户的电话号码,但 eclipse 说 onUpdate 中不允许使用 getSharedPreferences。还有其他方法可以执行吗?

我该怎么办?

我的代码:

public class Main extends AppWidgetProvider {
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        // TODO Auto-generated method stub
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        for(int i=0 ; i<appWidgetIds.length ; i++)
        {
            SharedPreferences details = getSharedPreferences("OPERATOR", 0);
            int appWidgetId = appWidgetIds[i];
            String phNumber = "5554574";

            Intent intent = new Intent(Intent.ACTION_CALL);
            intent.setData(Uri.parse("tel:"+(phNumber)));
            PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);

            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.main);
            views.setOnClickPendingIntent(R.id.button1, pending);

            appWidgetManager.updateAppWidget(appWidgetId, views);

        }
    }
}

【问题讨论】:

  • 在广播接收器中调用你的方法。在 Manifest-

标签: android android-widget sharedpreferences


【解决方案1】:

用途:

SharedPreferences details = context.getSharedPreferences("OPERATOR", 0);

SharedPreferences 必须从上下文中获取。 onUpdate(...) 为您提供上下文。

您更改后的代码应如下所示:

public class Main extends AppWidgetProvider {
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        // TODO Auto-generated method stub
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        for(int i=0 ; i<appWidgetIds.length ; i++)
        {
            SharedPreferences details = context.getSharedPreferences("OPERATOR", 0);
            int appWidgetId = appWidgetIds[i];
            String phNumber = "5554574";

            Intent intent = new Intent(Intent.ACTION_CALL);
            intent.setData(Uri.parse("tel:"+(phNumber)));
            PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);

            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.main);
            views.setOnClickPendingIntent(R.id.button1, pending);

            appWidgetManager.updateAppWidget(appWidgetId, views);

        }
    }
}

请试一试。

【讨论】:

  • 我这样保存我的共享首选项:
  • SharedPreferences details = getSharedPreferences("OPERATOR", 0); SharedPreferences.Editor 编辑器 = details.edit(); editor.putString("操作员", "hamrah"); editor.commit();
  • 是的,可以使用,但是必须从Context中获取SharedPreferences。 AppWidgetProvider 不是 Context,所以 Eclipse 显示错误。
  • 我的配置类是configure.JAVA,你的意思是我应该使用configure.getshared...但它不承认。
【解决方案2】:

AppWidgetProvider 不是上下文。但是您通常可以创建一个静态方法来访问应用程序的上下文,然后这样做

在 Android Manifest 文件中声明如下

<application android:name="com.example.MyApplication">

</application>

然后写类

public class MyApplication extends Application{

    private static Context context;

    public void onCreate(){
        super.onCreate();
        MyApplication.context = getApplicationContext();
    }

    public static Context getAppContext() {
        return MyApplication.context;
    }
}

然后就可以使用了

        SharedPreferences details = MyApplication.getAppContext().getSharedPreferences("OPERATOR", 0);

我还没有使用小部件,所以不能保证这个解决方案,但希望它有效!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多