Skip to content

Instantly share code, notes, and snippets.

@daichan4649
Created September 11, 2014 06:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daichan4649/ab78c38d7316b0a42515 to your computer and use it in GitHub Desktop.
Save daichan4649/ab78c38d7316b0a42515 to your computer and use it in GitHub Desktop.
wifi setting notification (for Android)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="daichan4649.notification"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="20" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/red"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="WifiSettingService" />
</application>
</manifest>
private void showWifiSettingNotification() {
String currentStatusText = WifiUtil.getCurrentWifiStatusText(getApplicationContext());
NotificationUtil.showWifiSettingNotification(getApplicationContext(), currentStatusText);
}
public class NotificationUtil {
private static final int REQ_CODE_SETTING = 4649;
private static final int NOTIFICATION_ID_WIFI = 1;
public static void showWifiSettingNotification(Context context, String currentStatusText) {
Intent settingIntent = new Intent(context, WifiSettingService.class);
PendingIntent settingPI = PendingIntent.getService(context, REQ_CODE_SETTING, settingIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new Builder(context);
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher));
String contentTitle = "WIFI STATUS";
builder.setContentTitle(contentTitle);
String contentText = String.format("wifi:%s", currentStatusText);
builder.setContentText(contentText);
String actionText = String.format("wifi(%s)", currentStatusText);
builder.addAction(R.drawable.ic_launcher, actionText, settingPI);
Notification notification = builder.build();
notification.flags |= Notification.FLAG_NO_CLEAR;
NotificationManagerCompat nm = NotificationManagerCompat.from(context);
nm.notify(NOTIFICATION_ID_WIFI, notification);
}
}
public class WifiSettingService extends Service {
@Override
public IBinder onBind(Intent paramIntent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
boolean currentStatus = WifiUtil.getCurrentWifiStatus(getApplicationContext());
// change wifi status
WifiUtil.toggleWifiStatus(getApplicationContext());
// update notification
String afterStatusText = WifiUtil.getWifiStatusText(!currentStatus);
NotificationUtil.showWifiSettingNotification(getApplicationContext(), afterStatusText);
// toast
String toastText = String.format("wifi: ", afterStatusText);
Toast.makeText(getApplicationContext(), toastText, Toast.LENGTH_SHORT).show();
// stop
stopSelf(startId);
return super.onStartCommand(intent, flags, startId);
}
}
public class WifiUtil {
public static String getCurrentWifiStatusText(Context context) {
boolean currentStatus = getCurrentWifiStatus(context);
return getWifiStatusText(currentStatus);
}
public static String getWifiStatusText(boolean status) {
return String.format("%s", status ? "ON" : "OFF");
}
public static boolean getCurrentWifiStatus(Context context) {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
return wifiManager.isWifiEnabled();
}
public static void toggleWifiStatus(Context context) {
boolean currentStatus = getCurrentWifiStatus(context);
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(!currentStatus);
// すぐには切り替わらない
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment