मेरे पास एक एंड्रॉइड ऐप है जिसे इंटरनेट से सिंक करने की आवश्यकता है, लेकिन जैसे ही फोन सो जाता है, मैं इंटरनेट तक नहीं पहुंच सकता। यह तब होता है जब उपयोगकर्ता "बैटरी मोड" का उपयोग करता है, जब यह 15 मिनट के बाद डेटा बंद कर देता है। मैंने एक टेस्ट ऐप लिखा और डेटा चालू कर दिया, लेकिन यह अभी भी सर्वर से कनेक्ट है।सोते समय एंड्रॉइड पर इंटरनेट चालू करें
मैं क्या करने की कोशिश की:
- जब मैं डेटा मैन्युअल रूप से बंद कर देते हैं, फिर ऐप्स पर यह हो रहा है और यह
- मैं भी Wakelock की कोशिश की, लेकिन यह मदद नहीं की काम करता है।
- अलार्म के रूप में उम्मीद काम करता है, भले ही आपका फ़ोन घंटे
मोटोरोला Atrix एंड्रॉयड 2.3.3 पर परीक्षण किया गया के लिए सोने के लिए चला जाता है। मैं वाईफ़ाई पर भरोसा नहीं कर सकता। वास्तविक जीवन में यह हर हफ्ते सिंक होगा। हम इसे कैसे संभव बना सकते हैं?
AlarmManager:
alarm_manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pending = PendingIntent.getBroadcast(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
alarm_manager.setRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis(), 15000, pending);
AlarmReceiver:
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("MYTAG", "RECEIVED getMobileDataEnabled: " + getMobileDataEnabled(context));
if (!isOnline(context)) {
Log.d("MYTAG", "NO INET");
if (turnOnInet(context)) {
Log.d("MYTAG", "INET IS ON");
}
}
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://xxx.xxx.xxx.xxx/ping/pong/moto/");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("short_code", "ROFL"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
Log.d("MYTAG", "POST FINISHED");
}
catch (Exception e) {
Log.e("MYTAG", "MYTAG", e);
}
}
public boolean isOnline(Context context) {
ConnectivityManager cm = (ConnectivityManager)context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null){
Log.d("MYTAG", "isAvailable: "+netInfo.isAvailable());
}
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}
public boolean turnOnInet(Context context) {
ConnectivityManager mgr = (ConnectivityManager)context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
if (mgr == null) {
Log.d("MYTAG", "ConnectivityManager == NULL");
return false;
}
try {
Method setMobileDataEnabledMethod = mgr.getClass().getDeclaredMethod("setMobileDataEnabled", boolean.class);
if (null == setMobileDataEnabledMethod) {
Log.d("MYTAG", "setMobileDataEnabledMethod == null");
return false;
}
setMobileDataEnabledMethod.invoke(mgr, true);
}
catch(Exception e) {
Log.e("MYTAG", "MYTAG", e);
return false;
}
return true;
}
private boolean getMobileDataEnabled(Context context) {
ConnectivityManager mgr = (ConnectivityManager)context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
if (mgr == null) {
Log.d("MYTAG", "getMobileDataEnabled ConnectivityManager == null");
return false;
}
try {
Method method = mgr.getClass().getMethod("getMobileDataEnabled");
return (Boolean) method.invoke(mgr);
} catch (Exception e) {
Log.e("MYTAG", "MYTAG", e);
return false;
}
}
}
AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
कृपया अपने उपयोगकर्ताओं की बैटरी को न मारें। – SLaks
"वास्तविक जीवन" में यह हर सप्ताह सिंक होगा – programmersbook
क्या आपने RTC_WAKEUP के बजाय आरटीसी ध्वज का उपयोग करने का प्रयास किया है? चूंकि वेकअप को मजबूर नहीं किया जाता है, इसलिए ढांचा नेटवर्क पर स्विच हो जाएगा। चूंकि आप एक सप्ताह में सिंक का उपयोग कर रहे हैं, इसलिए आपको WAKEUP ध्वज – nandeesh