要求:当我第一次在小部件上点击时,这将重定向到登录页面。一旦我成功登录,从下次开始,通过点击小部件,它应该直接带我到主页,无需再次重定向到登录页面(我想使用意图和共享首选项)。
我无法将主页上的小部件的意图传递给另一个java文件。当用户第一次启动窗口小部件时,这是我的看法:
public class MyWidgetProvider extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager
,int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
for(int i=0;i<appWidgetIds.length;i++){
int appWidgetId = appWidgetIds[i];
SharedPreferences settings = context.getSharedPreferences("MYPREFS",0);
//Create a pending intent for a widget click
Intent intent = new Intent(context,TheMainActivity.class);
//TheMainActivity is the class to which the intent is needed to be sent
intent.putExtra("logid",settings.getString("logid", ""));
intent.putExtra("password",settings.getString("password", ""));
PendingIntent pIntentNetworkInfo = PendingIntent.getActivity(context, 0,
intent, 0);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.activity_main);
remoteViews.setOnClickPendingIntent(R.id.update, pIntentNetworkInfo);
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
}
}
最初“logid”和“密码” NULL .Hence它将重定向到登录页面。但是一旦用户登录,然后从下一次开始运行小部件时,它应该自动重定向到主页。所以说,
if(logid=="xyz" && password=="abc")
然后它应该直接重定向到主页。我没有得到我应该为此目的编写代码的位置(即重定向到显示主页的另一个意图)。是否它应该是用onUpdate
方法或onEnabled
或哪一个写?
我是新手,所以如果解释不太好,请不要打扰我。
答案 0 :(得分:1)
您可以使用:
SharedPreferences settings = context.getSharedPreferences("MYPREFS",0);
String login=settings.getString("logid","0");
String pswrd=settings.getString("password","0");
if(login=="0" && pswrd=="0")
{
// Pass Intent to LOGIN Activity
}
else
{
// Pass intent to HOME Activity
}