更新
public class loginForm extends Activity
{
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView (R.layout.login);
receiver = new ConnectionReceiver();
registerReceiver(receiver,new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
}
private class ConnectionReceiver extends BroadcastReceiver{
private Timer mTimer;
private TimerTask mTimerTask;
@Override
public void onReceive(Context context, Intent intent) {
NetworkInfo info = intent.getParcelableExtra (ConnectivityManager.EXTRA_NETWORK_INFO);
if(null != info)
{
String state = getNetworkStateString(info.getState());
if(state.equals("Connected")){
mTimer = new Timer();
mTimerTask = new TimerTask() {
@Override
public void run() {
loginForm.this.runOnUiThread(new Runnable() {
@Override
public void run() {
//Toast.makeText(getBaseContext(), "Disenabled provider " + provider,
///Toast.LENGTH_SHORT).show();
try{
insertAllGpsInformation();
}
catch(Exception e)
{
Toast.makeText(getBaseContext(), "Your Net Connected or Not Login to Net"+"", Toast.LENGTH_LONG).show();
Log.e("Upload Picture Error:",e.getMessage());
}
}
});
}
};
mTimer.scheduleAtFixedRate(mTimerTask,180000,180000);
}
}
}
}
}
答案 0 :(得分:0)
注册ACTION_BOOT_COMPLETED
(see here for details)。启动时启动服务。
答案 1 :(得分:0)
您应该注册一个BroadcastReceiver并查找BOOT_COMPLETED Intent。
以下是一些详细信息的链接:http://androidgps.blogspot.com/2008/09/starting-android-service-at-boot-time.html
我是否正确理解了您的问题?
答案 2 :(得分:0)
使用BOOT COMPLETED Action的意图创建广播接收器。
请参阅以下链接以获取更多帮助:
您可以使用
public class BootReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
// TODO Auto-generated method stub
Log.i("BootReceiver :: Start Booting..");
Intent i = new Intent(context, StartService.class); // Start your service class
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
并使用androidmanifest.xml中的broadcastreciever作为
<receiver android:name=".receiver.BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>