/** * 获取手机联网状态 * * @param context * @return */ public static String getNetState(Context context) { TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); ConnectivityManager mConnectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = mConnectivity.getActiveNetworkInfo(); if (info == null || !mConnectivity.getBackgroundDataSetting()) { return null; } if (info.isConnected()) { int netType = info.getType(); int netSubtype = info.getSubtype(); if (netType == ConnectivityManager.TYPE_WIFI) { return "WIFI"; } else if (netType == ConnectivityManager.TYPE_MOBILE && netSubtype == TelephonyManager.NETWORK_TYPE_UMTS && !tm.isNetworkRoaming()) { return "3G"; } else if (netType == ConnectivityManager.TYPE_MOBILE) { return "GPRS"; } else { return "未知"; } } else { return null; } }
@Override protected void onHandleIntent(Intent intent) { ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); @SuppressWarnings("deprecation") boolean isNetworkAvailable = cm.getBackgroundDataSetting() && cm.getActiveNetworkInfo() != null; if (!isNetworkAvailable) { return; } SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); String query = prefs.getString(FlickrFetchr.RREF_SEARCH_QUERY, null); String lastResultId = prefs.getString(FlickrFetchr.PREF_LAST_RESULT_ID, null); ArrayList<GalleryItem> items; if (query == null) { items = FlickrFetchr.fetchItems(); } else { items = FlickrFetchr.search(query); } if (items.size() == 0) { return; } String resultId = items.get(0).getId(); if (!resultId.equals(lastResultId)) { Log.i(TAG, "Got a new result: " + resultId); Resources r = getResources(); PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, PhotoGalleryActivity.class), 0); Notification notification = new NotificationCompat.Builder(this) .setTicker(r.getString(R.string.new_pictures_title)) .setSmallIcon(android.R.drawable.ic_menu_report_image) .setContentTitle(r.getString(R.string.new_pictures_title)) .setContentText(r.getString(R.string.new_pictures_text)) .setContentIntent(pi) .setAutoCancel(true) .build(); NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); notificationManager.notify(0, notification); } else { Log.i(TAG, "Got an old result: " + resultId); } prefs.edit().putString(FlickrFetchr.PREF_LAST_RESULT_ID, resultId).commit(); }
public boolean isConnecting() { ConnectivityManager mConnectivity = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); TelephonyManager mTelephony = (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE); NetworkInfo info = mConnectivity.getActiveNetworkInfo(); if (info == null || !mConnectivity.getBackgroundDataSetting()) { return false; } int netType = info.getType(); int netSubtype = info.getSubtype(); if (netType == ConnectivityManager.TYPE_WIFI) { return info.isConnected(); } else if (netType == ConnectivityManager.TYPE_MOBILE && netSubtype == TelephonyManager.NETWORK_TYPE_UMTS && !mTelephony.isNetworkRoaming()) { return info.isConnected(); } else { return false; } }
public boolean isBackgroundDataEnabled() { ConnectivityManager manager = (ConnectivityManager) mApplicationContext.getSystemService(CONNECTIVITY_SERVICE); return manager.getBackgroundDataSetting(); }
/** * {@inheritDoc} Perform a checkin the specified venue. If the checkin fails, add it to the queue * and set an alarm to retry. * * <p>Query the checkin queue to see if there are pending checkins to be retried. */ @Override protected void onHandleIntent(Intent intent) { // Retrieve the details for the checkin to perform. String reference = intent.getStringExtra(PlacesConstants.EXTRA_KEY_REFERENCE); String id = intent.getStringExtra(PlacesConstants.EXTRA_KEY_ID); long timeStamp = intent.getLongExtra(PlacesConstants.EXTRA_KEY_TIME_STAMP, 0); // Check if we're running in the foreground, if not, check if // we have permission to do background updates. boolean backgroundAllowed = cm.getBackgroundDataSetting(); boolean inBackground = sharedPreferences.getBoolean(PlacesConstants.EXTRA_KEY_IN_BACKGROUND, true); if (reference != null && !backgroundAllowed && inBackground) { addToQueue(timeStamp, reference, id); return; } // Check to see if we are connected to a data network. NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting(); // If we're not connected then disable the retry Alarm, enable the Connectivity Changed Receiver // and add the new checkin directly to the queue. The Connectivity Changed Receiver will listen // for when we connect to a network and start this service to retry the checkins. if (!isConnected) { // No connection so no point triggering an alarm to retry until we're connected. alarmManager.cancel(retryQueuedCheckinsPendingIntent); // Enable the Connectivity Changed Receiver to listen for connection to a network // so we can commit the pending checkins. PackageManager pm = getPackageManager(); ComponentName connectivityReceiver = new ComponentName(this, ConnectivityChangedReceiver.class); pm.setComponentEnabledSetting( connectivityReceiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); // Add this checkin to the queue. addToQueue(timeStamp, reference, id); } else { // Execute the checkin. If it fails, add it to the retry queue. if (reference != null) { if (!checkin(timeStamp, reference, id)) addToQueue(timeStamp, reference, id); } // Retry the queued checkins. ArrayList<String> successfulCheckins = new ArrayList<String>(); Cursor queuedCheckins = contentResolver.query(QueuedCheckinsContentProvider.CONTENT_URI, null, null, null, null); try { // Retry each checkin. while (queuedCheckins.moveToNext()) { long queuedTimeStamp = queuedCheckins.getLong( queuedCheckins.getColumnIndex(QueuedCheckinsContentProvider.KEY_TIME_STAMP)); String queuedReference = queuedCheckins.getString( queuedCheckins.getColumnIndex(QueuedCheckinsContentProvider.KEY_REFERENCE)); String queuedId = queuedCheckins.getString( queuedCheckins.getColumnIndex(QueuedCheckinsContentProvider.KEY_ID)); if (queuedReference == null || checkin(queuedTimeStamp, queuedReference, queuedId)) successfulCheckins.add(queuedReference); } // Delete the queued checkins that were successful. if (successfulCheckins.size() > 0) { StringBuilder sb = new StringBuilder( "(" + QueuedCheckinsContentProvider.KEY_REFERENCE + "='" + successfulCheckins.get(0) + "'"); for (int i = 1; i < successfulCheckins.size(); i++) sb.append( " OR " + QueuedCheckinsContentProvider.KEY_REFERENCE + " = '" + successfulCheckins.get(i) + "'"); sb.append(")"); int deleteCount = contentResolver.delete( QueuedCheckinsContentProvider.CONTENT_URI, sb.toString(), null); Log.d(TAG, "Deleted: " + deleteCount); } // If there are still queued checkins then set a non-waking alarm to retry them. queuedCheckins.requery(); if (queuedCheckins.getCount() > 0) { long triggerAtTime = System.currentTimeMillis() + PlacesConstants.CHECKIN_RETRY_INTERVAL; alarmManager.set( AlarmManager.ELAPSED_REALTIME, triggerAtTime, retryQueuedCheckinsPendingIntent); } else alarmManager.cancel(retryQueuedCheckinsPendingIntent); } finally { queuedCheckins.close(); } } }
private void access$300() { boolean flag = true; boolean flag1; boolean flag2; boolean flag3; boolean flag4; boolean flag6; mSyncHandler.removeMessages(2); ConnectivityManager connectivitymanager = (ConnectivityManager) mContext.getSystemService("connectivity"); NetworkInfo networkinfo = connectivitymanager.getActiveNetworkInfo(); Log.d( "gp.PicasaSyncManager", (new StringBuilder("active network: ")).append(networkinfo).toString()); if (networkinfo == null) flag1 = false; else switch (networkinfo.getType()) { case 0: flag6 = false; if (!flag6) { flag1 = false; } else { flag1 = flag; } break; case 1: flag6 = flag; if (!flag6) { flag1 = false; } else { flag1 = flag; } case 2: flag6 = false; if (!flag6) { flag1 = false; } else { flag1 = flag; } break; case 3: flag6 = false; if (!flag6) { flag1 = false; } else { flag1 = flag; } break; case 4: flag6 = false; if (!flag6) { flag1 = false; } else { flag1 = flag; } break; case 5: flag6 = false; if (!flag6) { flag1 = false; } else { flag1 = flag; } break; case 6: flag6 = false; if (!flag6) { flag1 = false; } else { flag1 = flag; } break; default: flag6 = flag; if (!flag6) { flag1 = false; } else { flag1 = flag; } break; } if (flag1 != mHasWifiConnectivity) { mHasWifiConnectivity = flag1; flag2 = flag; } else { flag2 = false; } flag3 = false; if (networkinfo != null) { boolean flag5 = networkinfo.isRoaming(); flag3 = false; if (flag5) flag3 = flag; } if (flag3 != mIsRoaming) { mIsRoaming = flag3; flag2 = flag; } flag4 = connectivitymanager.getBackgroundDataSetting(); Log.d( "gp.PicasaSyncManager", (new StringBuilder("background data: ")).append(flag4).toString()); if (mBackgroundData != flag4) mBackgroundData = flag4; else flag = flag2; if (flag) updateTasksInternal(); return; }