Android打开常用相关设置

2014-01-08

开发中经常会遇到需要打开各种设置,比如用到网络的地方需要打开网络设置,需要用到GPS 的地方需要打开位置服务等等,我今天去官网扒了一下Settings相关的Action,写了个示例。

 

package net.chengyong.opensettings;

import android.content.Intent;
import android.provider.Settings;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity implements AdapterView.OnItemClickListener {

    private ListView lvSettings;
    private String[] textDisplay;
    private String[] textIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lvSettings = (ListView) findViewById(R.id.lvSettings);


        textDisplay = new String[]{
                Settings.ACTION_ACCESSIBILITY_SETTINGS,
                Settings.ACTION_ADD_ACCOUNT,
                Settings.ACTION_AIRPLANE_MODE_SETTINGS,
                Settings.ACTION_APN_SETTINGS,
                Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
                Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS,
                Settings.ACTION_APPLICATION_SETTINGS,
                Settings.ACTION_BLUETOOTH_SETTINGS,
                Settings.ACTION_CAPTIONING_SETTINGS,
                Settings.ACTION_CAST_SETTINGS,
                Settings.ACTION_DATA_ROAMING_SETTINGS,
                Settings.ACTION_DATE_SETTINGS,
                Settings.ACTION_DEVICE_INFO_SETTINGS,
                Settings.ACTION_DISPLAY_SETTINGS,
                Settings.ACTION_DREAM_SETTINGS,
                Settings.ACTION_HOME_SETTINGS,
                Settings.ACTION_INPUT_METHOD_SETTINGS,
                Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS,
                Settings.ACTION_INTERNAL_STORAGE_SETTINGS,
                Settings.ACTION_LOCALE_SETTINGS,
                Settings.ACTION_LOCATION_SOURCE_SETTINGS,
                Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS,
                Settings.ACTION_MANAGE_APPLICATIONS_SETTINGS,
                Settings.ACTION_MEMORY_CARD_SETTINGS,
                Settings.ACTION_NETWORK_OPERATOR_SETTINGS,
                Settings.ACTION_NFCSHARING_SETTINGS,
                Settings.ACTION_NFC_PAYMENT_SETTINGS,
                Settings.ACTION_NFC_SETTINGS,
                Settings.ACTION_PRINT_SETTINGS,
                Settings.ACTION_PRIVACY_SETTINGS,
                Settings.ACTION_QUICK_LAUNCH_SETTINGS,
                Settings.ACTION_SEARCH_SETTINGS,
                Settings.ACTION_SECURITY_SETTINGS,
                Settings.ACTION_SETTINGS,
                Settings.ACTION_SHOW_REGULATORY_INFO,
                Settings.ACTION_SOUND_SETTINGS,
                Settings.ACTION_SYNC_SETTINGS,
                Settings.ACTION_USAGE_ACCESS_SETTINGS,
                Settings.ACTION_USER_DICTIONARY_SETTINGS,
                Settings.ACTION_VOICE_INPUT_SETTINGS,
                Settings.ACTION_WIFI_IP_SETTINGS,
                Settings.ACTION_WIFI_SETTINGS,
                Settings.ACTION_WIRELESS_SETTINGS,
                Settings.AUTHORITY,
                Settings.EXTRA_ACCOUNT_TYPES,
                Settings.EXTRA_AUTHORITIES,
                Settings.EXTRA_INPUT_METHOD_ID
        };

        textIntent = new String[]{
                Settings.ACTION_ACCESSIBILITY_SETTINGS,
                Settings.ACTION_ADD_ACCOUNT,
                Settings.ACTION_AIRPLANE_MODE_SETTINGS,
                Settings.ACTION_APN_SETTINGS,
                Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
                Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS,
                Settings.ACTION_APPLICATION_SETTINGS,
                Settings.ACTION_BLUETOOTH_SETTINGS,
                Settings.ACTION_CAPTIONING_SETTINGS,
                Settings.ACTION_CAST_SETTINGS,
                Settings.ACTION_DATA_ROAMING_SETTINGS,
                Settings.ACTION_DATE_SETTINGS,
                Settings.ACTION_DEVICE_INFO_SETTINGS,
                Settings.ACTION_DISPLAY_SETTINGS,
                Settings.ACTION_DREAM_SETTINGS,
                Settings.ACTION_HOME_SETTINGS,
                Settings.ACTION_INPUT_METHOD_SETTINGS,
                Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS,
                Settings.ACTION_INTERNAL_STORAGE_SETTINGS,
                Settings.ACTION_LOCALE_SETTINGS,
                Settings.ACTION_LOCATION_SOURCE_SETTINGS,
                Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS,
                Settings.ACTION_MANAGE_APPLICATIONS_SETTINGS,
                Settings.ACTION_MEMORY_CARD_SETTINGS,
                Settings.ACTION_NETWORK_OPERATOR_SETTINGS,
                Settings.ACTION_NFCSHARING_SETTINGS,
                Settings.ACTION_NFC_PAYMENT_SETTINGS,
                Settings.ACTION_NFC_SETTINGS,
                Settings.ACTION_PRINT_SETTINGS,
                Settings.ACTION_PRIVACY_SETTINGS,
                Settings.ACTION_QUICK_LAUNCH_SETTINGS,
                Settings.ACTION_SEARCH_SETTINGS,
                Settings.ACTION_SECURITY_SETTINGS,
                Settings.ACTION_SETTINGS,
                Settings.ACTION_SHOW_REGULATORY_INFO,
                Settings.ACTION_SOUND_SETTINGS,
                Settings.ACTION_SYNC_SETTINGS,
                Settings.ACTION_USAGE_ACCESS_SETTINGS,
                Settings.ACTION_USER_DICTIONARY_SETTINGS,
                Settings.ACTION_VOICE_INPUT_SETTINGS,
                Settings.ACTION_WIFI_IP_SETTINGS,
                Settings.ACTION_WIFI_SETTINGS,
                Settings.ACTION_WIRELESS_SETTINGS,
                Settings.AUTHORITY,
                Settings.EXTRA_ACCOUNT_TYPES,
                Settings.EXTRA_AUTHORITIES,
                Settings.EXTRA_INPUT_METHOD_ID
        };
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, textDisplay);
        lvSettings.setAdapter(adapter);
        lvSettings.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        try {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setAction(textIntent[position]);
            startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this,"不适用当前手机",Toast.LENGTH_SHORT).show();
        }
    }

}

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/lvSettings"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:choiceMode="singleChoice" />
</RelativeLayout>

 

 
<h2>参考资料</h2>

http://developer.android.com/intl/zh-cn/reference/android/provider/Settings.html

无回应:“Android打开常用相关设置”

发表评论

电子邮件地址不会被公开。 必填项已用*标注