(001)android_gps设置开启和关闭
package com.alin.test;
import android.app.Activity;
import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class TestAndroidActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) this.findViewById(R.id.btn);
button.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
f_toggleGpsButton();
if (f_isGPSEnable())
{
Toast.makeText(TestAndroidActivity.this, "GPS处于开启状态", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(TestAndroidActivity.this, "GPS处于关闭状态", Toast.LENGTH_LONG).show();
}
}
});
}
public void f_toggleGpsButton()
{
Intent gpsIntent = new Intent();
gpsIntent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
gpsIntent.addCategory("android.intent.category.ALTERNATIVE");
gpsIntent.setData(Uri.parse("custom:3"));
try
{
PendingIntent.getBroadcast(this, 0, gpsIntent, 0).send();
}
catch (CanceledException e)
{
e.printStackTrace();
}
}
private boolean f_isGPSEnable()
{
String mGpsState = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
Log.v("GPS", mGpsState);
if(mGpsState.contains("gps"))
{
return true;
}
else
{
return false;
}
}
}
***万事万物都有裂痕,那是光照进来的地方***

浙公网安备 33010602011771号