Java 旋转时保留数据AppWidgetProvider
几天后,当屏幕旋转时,我的AppWidgetProvider出现了问题,我丢失了里面的所有信息,比如数据库中的信息。我曾尝试在屏幕旋转后使用后台服务来接收这些信息,但它不起作用 我在这里开始服务:Java 旋转时保留数据AppWidgetProvider,java,android,service,rotation,widget,Java,Android,Service,Rotation,Widget,几天后,当屏幕旋转时,我的AppWidgetProvider出现了问题,我丢失了里面的所有信息,比如数据库中的信息。我曾尝试在屏幕旋转后使用后台服务来接收这些信息,但它不起作用 我在这里开始服务: public void onReceive(Context context, Intent intent) { super.onReceive(context, intent); AppWidgetManager appWidgetManager = AppWidgetManager
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, WidgetProvider.class));
Log.i("V7", "V7 getAction " + intent.getAction());
if (intent.getAction().equals(ACTION_LAUNCH_APPLICATION)) {
Log.i("V7", "V7 Launch");
launchMainActivity(context);
} else if (intent.getAction().equals(UPDATE_ACTION_SIGN_IN)) {
Bundle extras = intent.getExtras();
String hour = extras.getString("hour");
String action = extras.getString("action");
String accountsName = "";
Integer accountId = null;
try {
accountId = extras.getInt("account");
if (accountId != null) {
accountsName = (connection.readJSON("name", String.valueOf(accountId), null, "account.analytic.account")).getString("name");
}
Intent intentService = new Intent(context, MyUpdateService.class);
intentService.putExtra("hour", hour);
intentService.putExtra("action", action);
intentService.putExtra("account", accountsName);
context.startService(intentService);
} catch (Exception e) {
accountsName = "";
}
这是我的服务课:
public static class MyUpdateService extends Service {
Intent intent;
@Override
public void onCreate() {
}
@Override
public void onStart(Intent intent, int startId) {
this.intent = intent;
super.onStart(intent, startId);
// Update the widget
RemoteViews remoteView = buildRemoteView(this);
// Push update to homescreen
pushUpdate(remoteView);
// No more updates so stop the service and free resources
stopSelf();
}
public RemoteViews buildRemoteView(Context context) {
int layoutID = R.layout.mainwidget2;
RemoteViews views = new RemoteViews(context.getPackageName(), layoutID);
// Code to build and update the remote view
views.setTextViewText(R.id.tvHour, context.getString(R.string.tvHour) + " " + intent.getStringExtra("hour"));
views.setTextViewText(R.id.tvLastAction, context.getString(R.string.tvLastAction) + " " + intent.getStringExtra("action"));
views.setTextViewText(R.id.tvAccount, intent.getStringExtra("account"));
return views;
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
RemoteViews remoteView = buildRemoteView(this);
// Push update to home screen
pushUpdate(remoteView);
}
private void pushUpdate(RemoteViews remoteView) {
ComponentName myWidget = new ComponentName(this, WidgetProvider.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(myWidget, remoteView);
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}
这是我的舱单:
<service android:name="android.timesheets.MyUpdateService" />
我的服务似乎从未启动
如果你有其他方法来保存我的信息,我知道
我希望你能帮助我