因此,这是一个带有引导接收器的Lollipop应用程序(5.0)。引导接收器启动一个活动(SplashActivity.class)。 SplashActivity的目的是在启动MainActivity.class之前获取所有必要的权限。
public class SplashActivity extends Activity {
private static boolean bPermission = false;
private static final int MY_PERMISSIONS_REQUEST = 100;
private static final String[] allRequestedPermissions = new String[] {
Manifest.permission.READ_CONTACTS,
Manifest.permission.SEND_SMS,
Manifest.permission.CALL_PHONE
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(getIntent().hasExtra("PERMISSION_FOR_SERVICE")) {
bPermission = true;
}
if (!checkAllRequestedPermissions()) {
ActivityCompat.requestPermissions(this, allRequestedPermissions, MY_PERMISSIONS_REQUEST);
}
else {
startMainActivity();
}
}
private boolean checkAllRequestedPermissions() {
for (String permission : allRequestedPermissions) {
if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}
private boolean isServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST: {
if (checkAllRequestedPermissions()) {
startMainActivity();
}
else {
finish();
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
private void startMainActivity() {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
if(bPermission) {
intent.putExtra("PERMISSION_FOR_SERVICE","_");
}
startActivity(intent);
}
}
在所有权限检查和请求(如果滚动到底部)之后最终发生的事情是启动MainActivity。
问题是我希望在bPermission == true的情况下不要启动MainActivity。如果bPermission == true,则倾向于以下行为:
private void startMainActivity() {
if(bPermission) {
startService(new Intent(this, SchedulerService.class));
finish();
}
else {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
intent.putExtra("PERMISSION_FOR_SERVICE", "_");
startActivity(intent);
}
}
但是,这会使应用程序崩溃。任何想法可能有什么问题吗?提前非常感谢
已解决: 我的服务有一个不知道的依赖项。在启动服务已修复问题之前初始化依赖项!谢谢@mike m。!!
答案 0 :(得分:0)
已解决:我的服务有一个依赖项无法识别。在启动服务已修复问题之前初始化依赖项!谢谢@Mike M。!!