我是Android应用的新手。当没有Wi-Fi连接时,我的应用程序通过从SQLite数据库读取数据来工作。应用程序的入口点是登录屏幕。
当没有Wi-Fi连接和缓存时,我们会弹出一条消息,要求设置Wi-Fi连接。一旦他们设置Wi-Fi,然后我们需要开始获得初始配置,以显示一些数据从服务器登录屏幕。
问题是,一旦我们设置了Wi-Fi,我不知道如何重新启动应用程序以从服务器获取数据。
发布于 2011-11-17 00:43:06
您并没有给我们更多的帮助,所以我只能给您一些高层次的建议。你所需要做的就是一旦你完成了wifi的设置,然后启动另一个意图来启动你的登录活动。因此,一旦完成wifi设置,请执行以下操作:
Intent loginActivity = new Intent(this, LoginActivity.class);
startActivity(loginActivity);
这里假设您从一个扩展了Context
的类调用,并且您的登录活动类的名称是LoginActivity
。
发布于 2011-11-17 03:09:31
如果没有,wifi会给他提示对话框,你应该使用startActivityForResult()
和
protected void onActivityResult(int requestCode, int resultCode,
Intent data)
这里有一些帮助你入门的教程:nice example
developer site
发布于 2011-11-19 08:24:53
除了我对@Kurtis的回答的评论之外,这里还有一段快速而肮脏的示例代码,你可以用它来监听Wifi状态的变化……
public class TestProjectActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Register for Wifi state changes.
this.registerReceiver(wifiChangedReceiver, new IntentFilter(
WifiManager.WIFI_STATE_CHANGED_ACTION));
}
// BroadcastReceiver that will get notified when the Wifi state changes.
private BroadcastReceiver wifiChangedReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
int extraWifiState = arg1.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
WifiManager.WIFI_STATE_UNKNOWN);
// No need to listen for all the states, but it might be interesting
// at a later point ;)
switch (extraWifiState) {
case WifiManager.WIFI_STATE_DISABLED:
showToast("Wifi disabled...");
break;
case WifiManager.WIFI_STATE_DISABLING:
showToast("Wifi disabling...");
break;
case WifiManager.WIFI_STATE_ENABLED:
showToast("Wifi enabled...");
break;
case WifiManager.WIFI_STATE_ENABLING:
showToast("Wifi enabling...");
break;
default:
break;
}
}
};
// Instead of showing a toast, you could launch a new activity.
private void showToast(String action) {
Toast.makeText(this, action, Toast.LENGTH_LONG).show();
}
// When leaving the activity that has registered for a broadcast, remember
// to unregister the broadcast or you'll get an exception at some point.
@Override
protected void onStop() {
super.onStop();
this.unregisterReceiver(wifiChangedReceiver);
}
}
有些人可能会争辩说,将BroadcastReceiver移到它自己的类中会更好。这可以通过创建您自己的类并通过BroadcastReceiver扩展该类来实现。
希望这能有所帮助;-)
https://stackoverflow.com/questions/8155197
复制相似问题