Enable/ Disable GPS programatically in Android
Step: 1 MainActivity.calss
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText tes;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tes = (EditText)findViewById(R.id.editText1);
Button tn = (Button)findViewById(R.id.button1);
tn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String se_vale = tes.getText().toString();
if(se_vale.equals("yes"))
{
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
sendBroadcast(intent);
Toast.makeText(getApplicationContext(), "Gps is Enabled", Toast.LENGTH_SHORT).show();
}
else if(se_vale.equals("no"))
{
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", false);
sendBroadcast(intent);
Toast.makeText(getApplicationContext(), "gps is disabled", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(), "Sorry", Toast.LENGTH_SHORT).show();
}
}
});
//super.loadUrl("file:///android_asset/www/ajaxcall.html");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Step:2 AndroidManifest.file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.cap"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
>
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Step:1 Image
Setp:2 ImageSetp3:image
No comments:
Post a Comment