android getsystemservice需要权限,activity manager - Android: getSystemService(Context.ACTIVITY_SERVICE) ...

在尝试通过getSystemService(Context.ACTIVITY_SERVICE)获取ActivityManager时,应用停止工作。调试时发现源代码与字节码不匹配,导致在ActivityThread.java的cleanUpPendingRemoveWindows()方法中出现问题。代码中启动了一个服务,该服务旨在查找错误状态的进程,但在此过程中遇到障碍。
摘要由CSDN通过智能技术生成

I am getting a strange problem which I am not able to understand as the app does not give any crash log or any other message which I can follow to search the problem.

I launching a service from my activity and in this service I am trying to find out the number of process which are in error state but when my code reaches to the line

ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);

then the app stops working. When I debugged my app then I found that after hitting the above line the debugger navigates to the file ActivityThread.java in cleanUpPendingRemoveWindows() with a message "Source code does not match the byte code". I am not able to understand why this is happening?

My code is:

import android.app.ActivityManager;

import android.app.Service;

import android.app.usage.UsageStats;

import android.app.usage.UsageStatsManager;

im

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是对代码的注释和优化: ```java package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.app.Presentation; import android.content.Context; import android.hardware.display.DisplayManager; import android.os.Bundle; import android.view.Display; import android.widget.TextView; // 继承自 Presentation 类,用于显示在外部显示设备上的内容 class MyPresentation extends Presentation { private TextView mText; public MyPresentation(Context context, Display display) { super(context, display); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 为这个 Presentation 设置布局 setContentView(R.layout.presentation_layout); // 获取布局中的 TextView,并设置显示文本 mText = findViewById(R.id.presentation_text); mText.setText("This is my presentation!"); } } public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 获取 DisplayManager 实例 DisplayManager displayManager = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE); // 获取所有的外部显示设备 Display[] displays = displayManager.getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION); // 如果有外部显示设备,则在第一个设备上显示内容 if (displays.length > 0) { MyPresentation presentation = new MyPresentation(this, displays[0]); presentation.show(); } } } ``` 优化:添加了注释,增强了代码可读性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值