我试图在我的SplashScreen中撤销我的应用程序的权限,但每次都崩溃了。我已经在我的onCreate中检查了sdk版本是否为> = 23。
SplashScreen.java
public class SplashScreen extends Activity {
// Splash screen timer
public static final String MyPREFERENCES = "MyPrefs";
SharedPreferences sharedpreferences;
MediaPlayer ringtone;
Context context;
private static int SPLASH_TIME_OUT = 500;
private Activity activity;
private static final int PERMISSION_REQUEST_CODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
context = getApplicationContext();
activity = this;
ConnectionDetector cd = new ConnectionDetector(getApplicationContext());
Boolean isInternetPresent = cd.isConnectingToInternet();
System.out.println(isInternetPresent);
if(isInternetPresent){
new Handler().postDelayed(new Runnable() {
/*
* Showing splash screen with a timer. This will be useful when you
* want to show case your app logo / company
*/
public void run() {
// This method will be executed once the timer is over
// Start your app main activity
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
// close this activity
finish();
}
}, SPLASH_TIME_OUT);
boolean granted=checkPermission();
Log.d("Permission already there","---"+granted );
/*if(!granted){
requestPermission();
}*/
if (Build.VERSION.SDK_INT >= 23) {
System.out.println("Build version is"+Build.VERSION.SDK_INT);
requestPermission();
}
}
else{
Toast.makeText(getApplicationContext(), "Not Connected to internet",Toast.LENGTH_SHORT).show();
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("No Internet Connection Available. Do you want to try again")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
Boolean status = NetworkUtil.getConnectivityStatusString(getApplicationContext());
if(status){
Intent i = new Intent(getApplicationContext(), SplashScreen.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
else{
// create alert dialog
AlertDialog alertDialog = builder.create();
// show it
alertDialog.show();
}
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Action for 'NO' Button
finish();
}
});
//Creating dialog box
AlertDialog alert = builder.create();
//Setting the title manually
alert.setTitle("No Internet");
alert.show();
}
}
private boolean checkPermission(){
int result = ContextCompat.checkSelfPermission(SplashScreen.this, Manifest.permission.ACCESS_FINE_LOCATION)+
ContextCompat.checkSelfPermission(SplashScreen.this, Manifest.permission.MODIFY_AUDIO_SETTINGS)+
ContextCompat.checkSelfPermission(SplashScreen.this, Manifest.permission.RECORD_AUDIO)+
ContextCompat.checkSelfPermission(SplashScreen.this, Manifest.permission.CAPTURE_AUDIO_OUTPUT)+
ContextCompat.checkSelfPermission(SplashScreen.this, Manifest.permission.DISABLE_KEYGUARD)+
ContextCompat.checkSelfPermission(SplashScreen.this, Manifest.permission.WAKE_LOCK)+
ContextCompat.checkSelfPermission(SplashScreen.this, Manifest.permission.CAPTURE_VIDEO_OUTPUT)+
ContextCompat.checkSelfPermission(SplashScreen.this, Manifest.permission.CALL_PHONE)+
ContextCompat.checkSelfPermission(SplashScreen.this, Manifest.permission.CAMERA)+
ContextCompat.checkSelfPermission(SplashScreen.this, Manifest.permission.CAPTURE_SECURE_VIDEO_OUTPUT)+
ContextCompat.checkSelfPermission(SplashScreen.this, Manifest.permission.ACCESS_NETWORK_STATE)+
ContextCompat.checkSelfPermission(SplashScreen.this, Manifest.permission.VIBRATE)+
ContextCompat.checkSelfPermission(SplashScreen.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)+
ContextCompat.checkSelfPermission(SplashScreen.this, Manifest.permission.GET_ACCOUNTS)+
ContextCompat.checkSelfPermission(SplashScreen.this, Manifest.permission.READ_CONTACTS)
;
if (result == PackageManager.PERMISSION_GRANTED){
System.out.println("Returning true");
return true;
} else {
System.out.println("Returning false");
return false;
}
}
private void requestPermission(){
Log.d("requestPermission ","Inside requestPermission");
Log.d("activity ",activity+"");
Log.d("activity ",Manifest.permission.ACCESS_FINE_LOCATION+"");
if (ActivityCompat.shouldShowRequestPermissionRationale(activity,Manifest.permission.ACCESS_FINE_LOCATION)&&
ActivityCompat.shouldShowRequestPermissionRationale(activity,Manifest.permission.MODIFY_AUDIO_SETTINGS)&&
ActivityCompat.shouldShowRequestPermissionRationale(activity,Manifest.permission.RECORD_AUDIO)&&
ActivityCompat.shouldShowRequestPermissionRationale(activity,Manifest.permission.CAPTURE_AUDIO_OUTPUT)&&
ActivityCompat.shouldShowRequestPermissionRationale(activity,Manifest.permission.DISABLE_KEYGUARD)&&
ActivityCompat.shouldShowRequestPermissionRationale(activity,Manifest.permission.WAKE_LOCK)&&
ActivityCompat.shouldShowRequestPermissionRationale(activity,Manifest.permission.CAPTURE_VIDEO_OUTPUT)&&
ActivityCompat.shouldShowRequestPermissionRationale(activity,Manifest.permission.CALL_PHONE)&&
ActivityCompat.shouldShowRequestPermissionRationale(activity,Manifest.permission.CAMERA)&&
ActivityCompat.shouldShowRequestPermissionRationale(activity,Manifest.permission.CAPTURE_SECURE_VIDEO_OUTPUT)&&
ActivityCompat.shouldShowRequestPermissionRationale(activity,Manifest.permission.ACCESS_NETWORK_STATE)&&
ActivityCompat.shouldShowRequestPermissionRationale(activity,Manifest.permission.WRITE_EXTERNAL_STORAGE)&&
ActivityCompat.shouldShowRequestPermissionRationale(activity,Manifest.permission.READ_CONTACTS)&&
ActivityCompat.shouldShowRequestPermissionRationale(activity,Manifest.permission.GET_ACCOUNTS)&&
ActivityCompat.shouldShowRequestPermissionRationale(activity,Manifest.permission.VIBRATE)){
Log.d("requestPermission","Inside if requestPermission");
} else {
Log.d("requestPermission","Inside else of requestPermission");
ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.MODIFY_AUDIO_SETTINGS,
Manifest.permission.RECORD_AUDIO,
Manifest.permission.CAPTURE_AUDIO_OUTPUT,
Manifest.permission.DISABLE_KEYGUARD,
Manifest.permission.WAKE_LOCK,
Manifest.permission.CAPTURE_VIDEO_OUTPUT,
Manifest.permission.CALL_PHONE,
Manifest.permission.CAMERA,
Manifest.permission.CAPTURE_SECURE_VIDEO_OUTPUT,
Manifest.permission.ACCESS_NETWORK_STATE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_CONTACTS,
Manifest.permission.GET_ACCOUNTS,
Manifest.permission.VIBRATE,
},PERMISSION_REQUEST_CODE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Snackbar.make(view,"Permission Granted, Now you can access location data.",Snackbar.LENGTH_LONG).show();
Toast.makeText(context, "Permission Granted, Now you can access location data.", Toast.LENGTH_LONG).show();
} else {
// Snackbar.make(view,"Permission Denied, You cannot access location data.",Snackbar.LENGTH_LONG).show();
Toast.makeText(context, "Permission Denied, You cannot access location data.", Toast.LENGTH_LONG).show();
}
break;
}
}
}
在我的日志中,我可以看到它的打印活动变量和其他一些变量。我不明白哪个变量变为null。 logs.txt
10-19 15:25:40.282: I/System.out(15747): Returning false
10-19 15:25:40.282: D/Permission already there(15747): ---false
10-19 15:25:40.282: D/requestPermission(15747): Inside requestPermission
10-19 15:25:40.282: D/AndroidRuntime(15747): Shutting down VM
10-19 15:25:40.293: E/AndroidRuntime(15747): FATAL EXCEPTION: main
10-19 15:25:40.293: E/AndroidRuntime(15747): Process: com.example, PID: 15747
10-19 15:25:40.293: E/AndroidRuntime(15747): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.app.Activity.shouldShowRequestPermissionRationale(java.lang.String)' on a null object reference
10-19 15:25:40.293: E/AndroidRuntime(15747): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
10-19 15:25:40.293: E/AndroidRuntime(15747): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
10-19 15:25:40.293: E/AndroidRuntime(15747): at android.app.ActivityThread.-wrap11(ActivityThread.java)
10-19 15:25:40.293: E/AndroidRuntime(15747): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
10-19 15:25:40.293: E/AndroidRuntime(15747): at android.os.Handler.dispatchMessage(Handler.java:102)
10-19 15:25:40.293: E/AndroidRuntime(15747): at android.os.Looper.loop(Looper.java:148)
10-19 15:25:40.293: E/AndroidRuntime(15747): at android.app.ActivityThread.main(ActivityThread.java:5443)
10-19 15:25:40.293: E/AndroidRuntime(15747): at java.lang.reflect.Method.invoke(Native Method)
10-19 15:25:40.293: E/AndroidRuntime(15747): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
10-19 15:25:40.293: E/AndroidRuntime(15747): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
10-19 15:25:40.293: E/AndroidRuntime(15747): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.app.Activity.shouldShowRequestPermissionRationale(java.lang.String)' on a null object reference
10-19 15:25:40.293: E/AndroidRuntime(15747): at android.support.v4.app.ActivityCompatApi23.shouldShowRequestPermissionRationale(ActivityCompat23.java:37)
10-19 15:25:40.293: E/AndroidRuntime(15747): at android.support.v4.app.ActivityCompat.shouldShowRequestPermissionRationale(ActivityCompat.java:363)
10-19 15:25:40.293: E/AndroidRuntime(15747): at com.example.MainActivity.requestPermission(MainActivity.java:140)
答案 0 :(得分:0)
尝试在SplashActivity
开始时不请求权限。在MainActivity
已经启动时请求它。我认为您的问题可能是您在应用程序执行与权限相关的所有代码之前完成SplashActivity
(因为您在延迟处理程序中启动下一个活动)。