我编辑了一些代码,使应用程序能够获取移动数据的基本信息。我也面临着问题。
应用程序在首次启动时在marshmallow中崩溃,但不允许access_coarse_location
权限错误。
建议如何解决问题以及此代码有什么问题。
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
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>
import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.telephony.CellLocation;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_PERMISSION = 1;
//Declaring View Object
public TextView Info;
public Button Refreshbtn;
@TargetApi(Build.VERSION_CODES.M)
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Refreshbtn = (Button) findViewById(R.id.Refresh);
Refreshbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DeviceData();
}
});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//Toast.makeText(this, "It is M Version", Toast.LENGTH_SHORT).show();
Askpermission();
} else {
// Toast.makeText(this, "it is below M", Toast.LENGTH_SHORT).show();
DeviceData();
}
}
private void Askpermission() {
List<String> permissionlist = new ArrayList<String>();
List<String> permissionneeded = new ArrayList<String>();
if (addPermission(permissionlist, Manifest.permission.ACCESS_COARSE_LOCATION))
permissionneeded.add("Location");
if (addPermission(permissionlist, Manifest.permission.ACCESS_FINE_LOCATION))
permissionneeded.add("Location");
if (addPermission(permissionlist, Manifest.permission.SEND_SMS))
permissionneeded.add("Send SMS");
if (addPermission(permissionlist, Manifest.permission.READ_PHONE_STATE))
permissionneeded.add("Read Phone Info");
if (permissionlist.size() > 0) {
if (permissionneeded.size() > 0) {
for (int i = 1; i < permissionneeded.size(); i++)
ActivityCompat.requestPermissions(this, permissionlist.toArray(new String[permissionlist.size()]), REQUEST_PERMISSION);
}
return;
}
DeviceData();
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case REQUEST_PERMISSION: {
Map<String, Integer> perms = new HashMap<String, Integer>();
// Initial
perms.put(Manifest.permission.ACCESS_FINE_LOCATION, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.READ_PHONE_STATE, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.SEND_SMS, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.ACCESS_COARSE_LOCATION, PackageManager.PERMISSION_GRANTED);
// Fill with results
for (int i = 0; i < permissions.length; i++)
perms.put(permissions[i], grantResults[i]);
// Check for ACCESS_FINE_LOCATION
if (perms.get(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.SEND_SMS) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
// All Permissions Granted
DeviceData();
} else {
// Permission Denied
Toast.makeText(MainActivity.this, "Some Permission is Denied", Toast.LENGTH_SHORT)
.show();
}
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
private boolean addPermission(List<String> permissionlist, String permission) {
if (ContextCompat.checkSelfPermission(MainActivity.this, permission) != PackageManager.PERMISSION_GRANTED) {
permissionlist.add(permission);
}
return true;
}
public void DeviceData() {
Info = (TextView) findViewById(R.id.Info);
//Creating object to use TelephonyManager Class methods to get required data.
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
CellLocation Devicelocation = tm.getCellLocation();
String DeviceId = tm.getDeviceId();
String Devicecountry = tm.getNetworkCountryIso();
String Deviceoperator = tm.getNetworkOperator();
String DeviceSimCountry = tm.getSimCountryIso();
String Simoperator = tm.getSimOperator();
String SimOperatorName = tm.getSimOperatorName();
String simSerialNumber = tm.getSimSerialNumber();
int Datanetwork = tm.getNetworkType();
String DataNetworkType = "";
String Softwareverion = tm.getDeviceSoftwareVersion();
String PhoneType = "";
int phonetype = tm.getPhoneType();
int datastate = tm.getDataState();
String DataState = "";
switch (phonetype) {
case (TelephonyManager.PHONE_TYPE_CDMA):
PhoneType = "CDMA";
break;
case (TelephonyManager.PHONE_TYPE_GSM):
PhoneType = "GSM";
break;
case (TelephonyManager.PHONE_TYPE_NONE):
PhoneType = "NONE";
break;
}
switch (Datanetwork) {
case (TelephonyManager.NETWORK_TYPE_EDGE):
DataNetworkType = "EDGE";
break;
case (TelephonyManager.NETWORK_TYPE_HSDPA):
DataNetworkType = "HSDPA";
break;
}
switch (datastate) {
case (TelephonyManager.DATA_DISCONNECTED):
DataState = "Data Disconnected";
break;
case (TelephonyManager.DATA_CONNECTING):
DataState = "Data Connecting";
break;
case (TelephonyManager.DATA_CONNECTED):
DataState = "Data Connected";
break;
}
boolean isRoaming = tm.isNetworkRoaming();
String AllDetails = "Phone Details:\n";
AllDetails += "\n Device Location:" + Devicelocation;
AllDetails += "\n Data Status:" + DataState;
AllDetails += "\n Data Network Type:" + DataNetworkType;
AllDetails += "\n Phone Type:" + PhoneType;
AllDetails += "\n Device IMEI No.:(Id)" + DeviceId;
AllDetails += "\n Device Country:" + Devicecountry;
AllDetails += "\n Device Operator:" + Deviceoperator;
AllDetails += "\n Device Sim Country:" + DeviceSimCountry;
AllDetails += "\n Sim Operator:" + Simoperator;
AllDetails += "\n Sim Operator Name:" + SimOperatorName;
AllDetails += "\n Sim Serial Number:" + simSerialNumber;
AllDetails += "\n Mobile Software Version:" + Softwareverion;
AllDetails += "\n Roaming Status:" + isRoaming;
Info.setText(AllDetails);
}
}