Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.annotation.SuppressLint;

import android.content.Context;
import android.content.Intent;

import android.os.Build;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;

public class Main {
    @SuppressWarnings("deprecation")
    @SuppressLint("NewApi")
    public static void toggleAirplane(Context context) {

        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
            return;

        int state = 0;
        try {
            if (isJBean())
                state = Settings.System.getInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON);
            else
                state = Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON);
        } catch (SettingNotFoundException e) {
            e.printStackTrace();
        }

        if (state == 0) {
            if (isJBean())
                Settings.System.putInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1);
            else
                Settings.System.putInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 1);
            Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
            intent.putExtra("state", true);
            context.sendBroadcast(intent);
        } else {
            if (isJBean())
                Settings.System.putInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0);
            else
                Settings.System.putInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0);
            Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
            intent.putExtra("state", false);
            context.sendBroadcast(intent);
        }
    }

    public static boolean isJBean() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
            return true;
        return false;
    }
}