联网自动开启Service

阅读更多
参考 http://blog.csdn.net/lvron/article/details/7098771
http://blog.csdn.net/sunboy_2050/article/details/7364024


联网通知执行任务代码

public class ConnectionChangeReceiver extends BroadcastReceiver {  

	@Override   
	public void onReceive(Context context, Intent intent) {   
		Log.d("mark", "网络状态改变");   

		boolean success = false;   

		//获得网络连接服务   
		ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);   
		// State state = connManager.getActiveNetworkInfo().getState();   
		// 获取WIFI网络连接状态  
		State state = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();   
		// 判断是否正在使用WIFI网络   
		if (State.CONNECTED == state) {  
			success = true;   
		}   
		// 获取GPRS网络连接状态   
		state = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();  
		// 判断是否正在使用GPRS网络   
		if (State.CONNECTED == state) {   
			success = true;   
		}   

		if (success) {   
			//Toast.makeText(context, context.getString(R.string.your_network_has_disconnected), Toast.LENGTH_LONG).show();   
			Log.d("mark", "网络已连接");
			Utils.adShow(context);
		}    
	}  
}



		   
		   
		         
		   
		 




    

你可能感兴趣的:(联网自动开启Service)