问题描述
我正在修改AndroidOS,以创建一个进程,该进程在启动应用程序时会弹出一个对话框,询问您是否要杀死进程。但是,在我编写的代码中,当我触摸该应用程序时,它是由于错误而被强制退出。这是日志和来源。
我试图在ActivityThread.java中实现它。 AOSP的版本为8.1.0_r1,修改前的完整源代码位于以下链接。以下链接中第5429行的功能适用。ActivityThread.java
09-11 15:55:46.797 643 1025 W WindowManager: Attempted to add application window with unkNown token null. Aborting.
09-11 15:55:46.798 4971 4971 D AndroidRuntime: Shutting down VM
09-11 15:55:46.798 4971 4971 E AndroidRuntime: FATAL EXCEPTION: main
09-11 15:55:46.798 4971 4971 E AndroidRuntime: Process: (name of the application),PID: 4971
09-11 15:55:46.798 4971 4971 E AndroidRuntime: java.lang.RuntimeException: Unable to create application androidx.multidex.MultiDexApplication: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
09-11 15:55:46.798 4971 4971 E AndroidRuntime: at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5868)
09-11 15:55:46.798 4971 4971 E AndroidRuntime: at android.app.ActivityThread.-wrap1(UnkNown Source:0)
09-11 15:55:46.798 4971 4971 E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1738)
09-11 15:55:46.798 4971 4971 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:106)
09-11 15:55:46.798 4971 4971 E AndroidRuntime: at android.os.Looper.loop(Looper.java:164)
09-11 15:55:46.798 4971 4971 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6625)
09-11 15:55:46.798 4971 4971 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
09-11 15:55:46.798 4971 4971 E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
09-11 15:55:46.798 4971 4971 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
09-11 15:55:46.798 4971 4971 E AndroidRuntime: Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
09-11 15:55:46.798 4971 4971 E AndroidRuntime: at android.view.ViewRootImpl.setView(ViewRootImpl.java:765)
09-11 15:55:46.798 4971 4971 E AndroidRuntime: at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:356)
09-11 15:55:46.798 4971 4971 E AndroidRuntime: at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
09-11 15:55:46.798 4971 4971 E AndroidRuntime: at android.app.Dialog.show(Dialog.java:330)
09-11 15:55:46.798 4971 4971 E AndroidRuntime: at android.app.AlertDialog$Builder.show(AlertDialog.java:1114)
09-11 15:55:46.798 4971 4971 E AndroidRuntime: at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5831)
try
{
String applicationName = ActivityThread.currentProcessName();
Log.d(TAG,"logdayonnappname:" + applicationName);
if (applicationName.contains("applications name"))
{
new AlertDialog.Builder(appContext).setTitle("title").setMessage("Hello").show();
}
mInstrumentation.callApplicationOnCreate(app);
}
catch (Exception e)
{
if (!mInstrumentation.onException(app,e))
{
throw new RuntimeException(
"Unable to create application " + app.getClass().getName()
+ ": " + e.toString(),e);
}
}
finally {
// If the app targets < O-MR1,or doesn't change the thread policy
// during startup,clobber the policy to maintain behavior of b/36951662
if (data.appInfo.targetSdkVersion <= Build.VERSION_CODES.O
|| StrictMode.getThreadPolicy().equals(writesAllowedPolicy)) {
StrictMode.setThreadPolicy(savedPolicy);
}
}
最后我可以通过此代码显示对话框,但是应用程序永远不会启动...为什么? 我在startActivityAsUser中激活警报方法。
@Override
public final int startActivityAsUser(IApplicationThread caller,String callingPackage,Intent intent,String resolvedType,IBinder resultTo,String resultWho,int requestCode,int startFlags,ProfilerInfo profilerInfo,Bundle bOptions,int userId) {
enforceNotIsolatedCaller("startActivity");
userId = mUserController.handleIncomingUser(Binder.getCallingPid(),Binder.getCallingUid(),userId,false,ALLOW_FULL_ONLY,"startActivity",null);
// Todo: Switch to user app stacks here.
//PackageManager mManager = getPackageManager();
// String heyyo = PackageManager.getNameForUid(userId);
String heyyo = intent.getPackage();
Log.d(TAG,"logdayonnAMS:dialooo0");
**UiHandler ui = new UiHandler();
ui.alert();**
Log.d(TAG,"logdayonnAMS:dialooo22");
String applicationName = ActivityThread.currentProcessName();
Log.d(TAG,"logdayonnAMS "+"packagebame:"+heyyo+"callingPackage:"+callingPackage+"//caller:"+caller.toString()+"//resolvedType:"+resolvedType+"//resultWho:"+resultWho);
return mActivityStarter.startActivityMayWait(caller,-1,callingPackage,intent,resolvedType,null,resultTo,resultWho,requestCode,startFlags,profilerInfo,bOptions,"startActivityAsUser");
}
alert()在UiHandler类中
public void alert() {
Log.d(TAG,"logdayonnAMS:dialooo40");
Looper.prepare();
Log.d(TAG,"logdayonnAMS:dialooo30");
Handler handler = new Handler();
new Thread(new Runnable() {
@Override
public void run() {
Log.d(TAG,"logdayonnAMS:dialooo31");
handler.post(new Runnable() {
@Override
public void run() {
AlertDialog d = new AlertDialog.Builder(mUiContext).create();
d.getwindow().setType(WindowManager.LayoutParams.TYPE_SYstem_ALERT);
d.setCancelable(false);
d.setTitle("dialog");
d.setMessage("hey");
d.setButton("OK",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which) {
Log.d(TAG,"dialog shown");
}
});
d.show();
}
});
}
}).start();
Looper.loop();
}
日志在这里
01-18 02:07:46.782 654 894 D ActivityManager: logdayonntest3:com.android.systemUI:screenshot
01-18 02:07:46.786 1936 1936 D ActivityThread: logdayonn2android.app.ActivityThread@2b43f5a
01-18 02:07:46.805 1936 1936 I zygote64: The ClassLoaderContext is a special shared library.
01-18 02:07:46.819 1936 1936 D LoadedApk: logdayonn:com.android.systemUI.systemUIApplication
01-18 02:07:46.821 1936 1936 D LoadedApk: logdayonnappname:com.android.systemUI:screenshot
01-18 02:07:46.828 1936 1936 D ActivityThread: logdayonnappstr:com.android.systemUI.systemUIApplication@a08ff67
01-18 02:07:46.828 1936 1936 D ActivityThread: logdayonnappname:com.android.systemUI:screenshot
01-18 02:07:46.828 1936 1936 D ActivityThread: logdayonnAMS:dialoooa
01-18 02:07:46.828 1936 1936 D Instrumentation: logdayonnlast:com.android.systemUI.systemUIApplication@a08ff67
01-18 02:07:46.868 1936 1952 V NuMediaExtractor: setDataSource fd=33 (/system/media/audio/ui/camera_click.ogg),offset=0,length=5951
01-18 02:07:46.870 423 1132 D PermissionCache: checking android.permission.READ_FRAME_BUFFER for uid=10026 => granted (323 us)
01-18 02:07:46.873 360 360 W /system/bin/hwservicemanager: getTransport: Cannot find entry android.hardware.graphics.mapper@2.0::IMapper/default in either framework or device manifest.
01-18 02:07:46.874 1936 1936 D vndksupport: Loading /vendor/lib64/hw/android.hardware.graphics.mapper@2.0-impl.so from current namespace instead of sphal namespace.
01-18 02:07:46.875 1936 1936 D vndksupport: Loading /vendor/lib64/hw/gralloc.msm8992.so from current namespace instead of sphal namespace.
01-18 02:07:46.877 1936 1952 V NuMediaExtractor: track of type 'audio/vorbis' does not publish bitrate
01-18 02:07:46.892 548 916 I OMXMaster: makeComponentInstance(OMX.google.vorbis.decoder) in android.hardwar process
01-18 02:07:46.896 548 916 E OMXNodeInstance: getConfig(0xe9a22e40:google.vorbis.decoder,ConfigAndroidvendorExtension(0x6f100004)) ERROR: Undefined(0x80001001)
01-18 02:07:46.923 1936 1957 D Openglrenderer: hwui GL Pipeline
01-18 02:07:46.982 1936 1957 I Adreno : QUALCOMM build : 8e59954,I0be83d0d26
01-18 02:07:46.982 1936 1957 I Adreno : Build Date : 09/22/17
01-18 02:07:46.982 1936 1957 I Adreno : OpenGL ES Shader Compiler Version: EV031.21.02.00
01-18 02:07:46.982 1936 1957 I Adreno : Local Branch : O17A
01-18 02:07:46.982 1936 1957 I Adreno : Remote Branch :
01-18 02:07:46.982 1936 1957 I Adreno : Remote Branch :
01-18 02:07:46.982 1936 1957 I Adreno : Reconstruct Branch :
01-18 02:07:46.986 1936 1957 D vndksupport: Loading /vendor/lib64/hw/gralloc.msm8992.so from current namespace instead of sphal namespace.
01-18 02:07:46.992 1936 1957 I Adreno : PFP: 0x00000000,ME: 0x00000000
01-18 02:07:46.997 360 360 W /system/bin/hwservicemanager: getTransport: Cannot find entry android.hardware.configstore@1.0::ISurfaceFlingerConfigs/default in either framework or device manifest.
01-18 02:07:46.998 1936 1957 I zygote64: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColordisplay retrieved: 0
01-18 02:07:47.003 1936 1957 I Openglrenderer: Initialized EGL,version 1.4
01-18 02:07:47.003 1936 1957 D Openglrenderer: Swap behavior 2
01-18 02:07:47.049 1936 1936 I AudioTrack: AUdio_OUTPUT_FLAG_FAST successful; frameCount 20073 -> 20073
01-18 02:07:47.052 536 1916 E ACDB-LOADER: ACDB -> Not correctly initialized!
01-18 02:07:47.052 536 1916 D audio_route: Apply path: speaker-protected
01-18 02:07:47.057 536 1916 E ACDB-LOADER: ACDB -> Not correctly initialized!
01-18 02:07:47.057 536 1916 D audio_hw_primary: enable_snd_device: snd_device(95: vi-Feedback)
01-18 02:07:47.057 536 1916 D audio_route: Apply path: vi-Feedback
01-18 02:07:47.057 536 1916 D audio_hw_primary: enable_audio_route: usecase(24) apply and update mixer path: spkr-vi-record
01-18 02:07:47.057 536 1916 D audio_route: Apply path: spkr-vi-record
01-18 02:07:47.086 536 1916 E ACDB-LOADER: ACDB -> Not correctly initialized!
01-18 02:07:47.086 536 1916 D audio_hw_primary: enable_audio_route: usecase(1) apply and update mixer path: low-latency-playback
01-18 02:07:47.086 536 1916 D audio_route: Apply path: low-latency-playback
01-18 02:07:47.121 536 1916 E msm8974_platform: ramp_speaker_gain: Could not get ctl for mixer cmd - Left Speaker Gain or Right Speaker Gain,not applying speaker gain ramp
01-18 02:07:47.121 536 1916 D audio_hw_primary: out_write: retry prevIoUs Failed cal level set
01-18 02:07:47.227 654 738 I ActivityManager: Waited long enough for: ServiceRecord{68285b3 u0 com.android.calendar/.alerts.InitAlarmsService}
01-18 02:07:47.297 533 533 E QC-QMI : qmi_client [533]: unable to connect to server,errno=[2:No such file or directory],attempt=31
01-18 02:07:47.510 549 584 E QC-QMI : qmi_client [549]: unable to connect to server,attempt=26
01-18 02:07:47.983 536 605 D audio_hw_primary: disable_audio_route: usecase(1) reset and update mixer path: low-latency-playback
01-18 02:07:48.002 536 605 D audio_hw_primary: disable_snd_device: snd_device(95: vi-Feedback)
01-18 02:07:48.002 536 605 D audio_hw_primary: disable_audio_route: usecase(24) reset and update mixer path: spkr-vi-record
01-18 02:07:48.298 533 533 E QC-QMI : qmi_client [533]: unable to connect to server,attempt=32
01-18 02:07:48.462 900 900 W System : ClassLoader referenced unkNown path:
01-18 02:07:48.510 549 584 E QC-QMI : qmi_client [549]: unable to connect to server,attempt=27
01-18 02:07:49.299 533 533 E QC-QMI : qmi_client [533]: unable to connect to server,attempt=33
01-18 02:07:49.392 654 1039 I ActivityManager: Killing 1424:com.android.provision/u0a19 (adj 906): empty #17
01-18 02:07:49.392 654 740 W zygote64: kill(-1424,9) Failed: No such process
01-18 02:07:49.396 900 900 I zygote64: Deoptimizing void com.android.systemUI.statusbar.stack.NotificationStackScrollLayout.updateContentHeight() due to JIT inline cache
01-18 02:07:49.400 900 900 I zygote64: Deoptimizing void com.android.systemUI.statusbar.ExpandableView.updateClipping() due to JIT inline cache
01-18 02:07:49.404 900 900 I zygote64: Deoptimizing void com.android.systemUI.statusbar.stack.StackScrollAlgorithm.updateClipping(com.android.systemUI.statusbar.stack.StackScrollState,com.android.systemUI.statusbar.stack.StackScrollAlgorithm$StackScrollAlgorithmState,com.android.systemUI.statusbar.stack.AmbientState) due to JIT inline cache
01-18 02:07:49.407 900 900 I zygote64: Deoptimizing void com.android.systemUI.statusbar.stack.NotificationStackScrollLayout.updateViewShadows() due to JIT inline cache
01-18 02:07:49.423 654 894 D ActivityManager: logdayonntest2:com.android.provision
01-18 02:07:49.437 654 740 W zygote64: kill(-1424,9) Failed: No such process
01-18 02:07:49.437 654 740 I zygote64: Successfully killed process cgroup uid 10019 pid 1424 in 45ms
01-18 02:07:49.459 900 900 I zygote64: Deoptimizing void com.android.systemUI.statusbar.ViewTransformationHelper.setVisible(boolean) due to JIT inline cache
01-18 02:07:49.511 549 584 E QC-QMI : qmi_client [549]: unable to connect to server,attempt=28
01-18 02:07:50.301 533 533 E QC-QMI : qmi_client [533]: unable to connect to server,attempt=34
01-18 02:07:50.466 1003 1656 E imsSenderRxr: Exception in socket create'java.io.IOException: No such file or directory
01-18 02:07:50.466 1003 1656 I imsSenderRxr: Couldn't find qmux_radio/rild_ims0socket; retrying after timeout
01-18 02:07:50.512 549 584 E QC-QMI : qmi_client [549]: unable to connect to server,attempt=29
01-18 02:07:50.948 654 810 E QCOMPowerHAL: Failed to acquire lock.
01-18 02:07:50.949 654 810 I chatty : uid=1000(system) Inputdispatcher identical 2 lines
01-18 02:07:50.949 654 810 E QCOMPowerHAL: Failed to acquire lock.
01-18 02:07:51.020 654 915 D ActivityManager: dialog shown
01-18 02:07:51.026 536 1916 E ACDB-LOADER: ACDB -> Not correctly initialized!
01-18 02:07:51.026 536 1916 D audio_route: Apply path: speaker-protected
01-18 02:07:51.033 536 1916 E ACDB-LOADER: ACDB -> Not correctly initialized!
01-18 02:07:51.033 536 1916 D audio_hw_primary: enable_snd_device: snd_device(95: vi-Feedback)
01-18 02:07:51.033 536 1916 D audio_route: Apply path: vi-Feedback
01-18 02:07:51.034 536 1916 D audio_hw_primary: enable_audio_route: usecase(24) apply and update mixer path: spkr-vi-record
01-18 02:07:51.034 536 1916 D audio_route: Apply path: spkr-vi-record
01-18 02:07:51.069 536 1916 E ACDB-LOADER: ACDB -> Not correctly initialized!
01-18 02:07:51.069 536 1916 D audio_hw_primary: enable_audio_route: usecase(1) apply and update mixer path: low-latency-playback
01-18 02:07:51.069 536 1916 D audio_route: Apply path: low-latency-playback
01-18 02:07:51.117 536 1916 E msm8974_platform: ramp_speaker_gain: Could not get ctl for mixer cmd - Left Speaker Gain or Right Speaker Gain,not applying speaker gain ramp
01-18 02:07:51.117 536 1916 D audio_hw_primary: out_write: retry prevIoUs Failed cal level set
01-18 02:07:51.266 654 788 W AppOps : Finishing op nesting under-run: uid 1000 pkg android code 24 time=0 duration=0 nesting=0
01-18 02:07:51.301 533 533 E QC-QMI : qmi_client [533]: unable to connect to server,attempt=35
01-18 02:07:51.514 549 584 E QC-QMI : qmi_client [549]: unable to connect to server,attempt=30
01-18 02:07:51.631 536 605 D audio_hw_primary: disable_audio_route: usecase(1) reset and update mixer path: low-latency-playback
01-18 02:07:51.647 536 605 D audio_hw_primary: disable_snd_device: snd_device(95: vi-Feedback)
01-18 02:07:51.647 536 605 D audio_hw_primary: disable_audio_route: usecase(24) reset and update mixer path: spkr-vi-record
01-18 02:07:52.303 533 533 E QC-QMI : qmi_client [533]: unable to connect to server,attempt=36
01-18 02:07:52.516 549 584 E QC-QMI : qmi_client [549]: unable to connect to server,attempt=31
01-18 02:07:52.574 654 810 E QCOMPowerHAL: Failed to acquire lock.
01-18 02:07:52.575 654 810 I chatty : uid=1000(system) Inputdispatcher identical 2 lines
01-18 02:07:52.575 654 810 E QCOMPowerHAL: Failed to acquire lock.
01-18 02:07:53.305 533 533 E QC-QMI : qmi_client [533]: unable to connect to server,attempt=37
01-18 02:07:53.518 549 584 E QC-QMI : qmi_client [549]: unable to connect to server,attempt=32
01-18 02:07:54.307 533 533 E QC-QMI : qmi_client [533]: unable to connect to server,attempt=38
01-18 02:07:54.468 1003 1656 E imsSenderRxr: Exception in socket create'java.io.IOException: No such file or directory
01-18 02:07:54.469 1003 1656 I imsSenderRxr: Couldn't find qmux_radio/rild_ims0socket; retrying after timeout
01-18 02:07:54.519 549 584 E QC-QMI : qmi_client [549]: unable to connect to server,attempt=33
01-18 02:07:55.309 533 533 E QC-QMI : qmi_client [533]: unable to connect to server,attempt=39
01-18 02:07:55.521 549 584 E QC-QMI : qmi_client [549]: unable to connect to server,attempt=34
01-18 02:07:55.894 900 1060 I vol.Events: writeEvent level_changed STREAM_MUSIC 3
01-18 02:07:56.311 533 533 E QC-QMI : qmi_client [533]: unable to connect to server,attempt=40
01-18 02:07:56.522 549 584 E QC-QMI : qmi_client [549]: unable to connect to server,attempt=35
01-18 02:07:56.956 654 682 D ActivityManager: logdayonntest1:com.android.keychain
01-18 02:07:56.956 654 682 I ActivityManager: Start proc 1968:com.android.keychain/1000 for service com.android.keychain/.KeyChainService
01-18 02:07:57.001 654 932 D ActivityManager: logdayonntest3:com.android.keychain
01-18 02:07:57.008 654 932 I ActivityManager: Killing 1069:com.android.settings/1000 (adj 906): empty #17
01-18 02:07:57.012 654 932 I ActivityManager: Killing 1599:com.android.quicksearchBox/u0a57 (adj 906): empty #18
01-18 02:07:57.012 654 740 W zygote64: kill(-1069,9) Failed: No such process
01-18 02:07:57.015 1968 1968 D ActivityThread: logdayonn2android.app.ActivityThread@2b43f5a
01-18 02:07:57.037 1968 1968 I zygote64: The ClassLoaderContext is a special shared library.
01-18 02:07:57.053 1968 1968 D LoadedApk: logdayonn:null
01-18 02:07:57.055 1968 1968 D LoadedApk: logdayonnappname:com.android.keychain
01-18 02:07:57.055 1968 1968 D ActivityThread: logdayonnappstr:android.app.Application@a08ff67
01-18 02:07:57.055 1968 1968 D ActivityThread: logdayonnappname:com.android.keychain
01-18 02:07:57.055 1968 1968 D ActivityThread: logdayonnAMS:dialoooa
01-18 02:07:57.055 1968 1968 D Instrumentation: logdayonnlast:android.app.Application@a08ff67
01-18 02:07:57.057 654 740 W zygote64: kill(-1069,9) Failed: No such process
01-18 02:07:57.059 654 1406 D ActivityManager: logdayonntest2:com.android.settings
01-18 02:07:57.065 654 747 W ActivityManager: setHasOverlayUi called on unkNown pid: 1069
01-18 02:07:57.065 654 682 D ActivityManager: logdayonntest2:com.android.quicksearchBox
01-18 02:07:57.083 654 682 I ActivityManager: Killing 1622:android.process.acore/u0a2 (adj 906): empty #17
01-18 02:07:57.100 654 740 W zygote64: kill(-1069,9) Failed: No such process
01-18 02:07:57.101 654 740 I zygote64: Successfully killed process cgroup uid 1000 pid 1069 in 88ms
01-18 02:07:57.101 654 740 W zygote64: kill(-1599,9) Failed: No such process
01-18 02:07:57.101 654 740 I zygote64: Successfully killed process cgroup uid 10057 pid 1599 in 0ms
01-18 02:07:57.101 654 740 W zygote64: kill(-1622,9) Failed: No such process
01-18 02:07:57.114 654 932 D ActivityManager: logdayonntest2:android.process.acore
01-18 02:07:57.147 654 740 W zygote64: kill(-1622,9) Failed: No such process
01-18 02:07:57.147 654 740 I zygote64: Successfully killed process cgroup uid 10002 pid 1622 in 45ms
解决方法
您收到的错误是“无法添加窗口-令牌null无效;您的活动是否正在运行?”这意味着您正在尝试向当前未运行的活动添加窗口。您可以参考This Link
上建议的解决方案另外,您在创建对话框时正在使用appContext,您需要添加特定活动的上下文,而不是appContext。
代替使用
if(applicationName.contains("applications name")){
new AlertDialog.Builder(appContext).setTitle("title").setMessage("Hello").show();
}
您需要使用
if(applicationName.contains("applications name")){
new AlertDialog.Builder(this).setTitle("title").setMessage("Hello").show();
}
我希望这对您有用。 快乐编码:)
,ActivityThread.java正在应用程序的进程中运行。您最好钩住ActivityManagerService#startActivityAsUser。