在这里,当我到达特定位置时,我正试图将手机更改为静音模式。
这是一个大学项目,在这里,我试图在进入大学校园时将设备置于静音模式。
这是我的MainActivity文件。
import android.Manifest;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.media.AudioManager;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final int REQUEST_LOCATION = 1;
Button button;
TextView textView;
LocationManager locationManager;
public double lattitude,longitude;
public AudioManager am;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
textView = (TextView)findViewById(R.id.text_location);
button = (Button)findViewById(R.id.button_location);
button.setOnClickListener(this);
if(lattitude == 21.92000007629394 || longitude == 82.77999877929686){
am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
}
else if(lattitude == 18.587765599999997 || longitude == 73.7407158){
am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
}
}
@Override
public void onClick(View view) {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
buildAlertMessageNoGps();
} else if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
getLocation();
}
}
private void getLocation() {
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission
(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
} else {
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Location location1 = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location location2 = locationManager.getLastKnownLocation(LocationManager. PASSIVE_PROVIDER);
if (location != null) {
double latti = location.getLatitude();
double longi = location.getLongitude();
lattitude = latti;
longitude = longi;
textView.setText("Your current location is"+ "\n" + "Lattitude = " + lattitude
+ "\n" + "Longitude = " + longitude);
} else if (location1 != null) {
double latti = location1.getLatitude();
double longi = location1.getLongitude();
lattitude = latti;
longitude = longi;
textView.setText("Your current location is"+ "\n" + "Lattitude = " + lattitude
+ "\n" + "Longitude = " + longitude);
} else if (location2 != null) {
double latti = location2.getLatitude();
double longi = location2.getLongitude();
lattitude = latti;
longitude = longi;
textView.setText("Your current location is"+ "\n" + "Lattitude = " + lattitude
+ "\n" + "Longitude = " + longitude);
}else{
Toast.makeText(this,"Unable to Trace your location",Toast.LENGTH_SHORT).show();
}
}
}
protected void buildAlertMessageNoGps() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Please Turn ON your GPS Connection")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int id) {
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
}
}
我尝试将代码设置为在不同的地方将手机更改为静音模式,例如在打印纬度和经度之后以及在调用getLocation()之前,但是其中任何一个都会崩溃应用程序。
这是我的清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.idks.locationsilent">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>
</manifest>
在最近的API中,我们需要访问应用程序来更改手机配置文件。
我只需要添加这个片段。
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&& !notificationManager.isNotificationPolicyAccessGranted()) {
Intent intent = new Intent(
android.provider.Settings
.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
startActivity(intent);
}