简体   繁体   English

Android应用程序可防止设备锁定屏幕并进入休眠状态

[英]Android application prevents device from locking screen and sleeping

My Android application being created by Android Studio now unintentionally prevents my Galaxy device from locking screen and sleeping. 现在,由Android Studio创建的我的Android应用程序无意间阻止了我的Galaxy设备锁定屏幕和进入睡眠状态。 Requested permissions are following: 请求的权限如下:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Earlier I included permission 之前我包含了许可

<!--<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>-->

but now this line is commented. 但现在此行已被注释。 Only INTERNET and ACCESS_NETWORK_STATE permissions are now requested. 现在仅请求INTERNETACCESS_NETWORK_STATE权限。 I tried to explicitly remove (uninstall) my application from device and install it again, but the application (when launched and being active) doesn't allow the device to sleep. 我试图从设备上明确删除(卸载)我的应用程序,然后再次安装,但是该应用程序(在启动并处于活动状态时)不允许设备进入睡眠状态。 When other app is active (my application is launched but inactive) the device comes to sleep in defined time. 当其他应用程序处于活动状态(我的应用程序已启动但处于非活动状态)时,设备将在规定的时间内进入睡眠状态。 The application use PendingIntent , Broadcast and AlarmManager classes, but the device doesn't sleep even if no method of these classes are called. 该应用程序使用PendingIntentBroadcastAlarmManager类,但是即使没有调用这些类的方法,设备也不会休眠。 WakeLock class is not used . 不使用 WakeLock类。

AndroidManifest.xml is following: AndroidManifest.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="spectorsky.timer">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <!--<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>-->
    <application

        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name="spectorsky.timer.MainActivity"
            android:label="@string/app_name"
            android:windowSoftInputMode="stateAlwaysHidden"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name="MyAlarmReceiver" />
    </application>

Here is the receiver class: 这是接收器类:

public class MyAlarmReceiver extends BroadcastReceiver {

    public MyAlarmReceiver() {
        super();
        }

        public void onReceive(Context context, Intent intent)
        {
            int channelNumber=intent.getIntExtra("channelNumber",13);
            String ipAddress=intent.getStringExtra("ipAddress");
            int nChannel=intent.getIntExtra("nChannel",13);
            intent.getParcelableExtra("messenger");
            new TVTimerAsync(ipAddress,nChannel,m, context).execute(MainActivity.digitReader(channelNumber));
        }  
}

And here is code invoking BroadCast: 这是调用BroadCast的代码:

MyIntent myIntentAlarm;
int myIntentId=0;
AlarmManager myAlarmManager;

................... ...................

 myAlarmManager.set(AlarmManager.RTC_WAKEUP, d.getTime(), PendingIntent.getBroadcast(MainActivity.this, myIntentId++,
                   myIntentAlarm, PendingIntent.FLAG_ONE_SHOT));

Thanks in advance for any idea. 预先感谢您的任何想法。

PS There are many posts about such problem, but I couldn't find something like my case. PS有很多关于这种问题的文章,但是我找不到类似我的情况的东西。

I've cleaned almost everything in the project: the application restored sleeping ability after deleting android:keepScreenOn attribute in ListView definition: 我已经清理了项目中的几乎所有内容:应用程序在删除ListView定义中的android:keepScreenOn属性后恢复了睡眠能力:

<ListView
        android:id="@+id/listIntents"
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:choiceMode="multipleChoice"
        android:focusable="false"
        android:focusableInTouchMode="false"

android:keepScreenOn="true"

        android:scrollIndicators="right"
        android:scrollbarAlwaysDrawVerticalTrack="true"
        android:scrollbarStyle="insideInset"
        android:textFilterEnabled="false"
        android:layout_height="match_parent"
        android:text="@string/deleteChosen">

</ListView>

I did hope that this attribute make the list to remain in front of other controls, and it will never be hidden. 我确实希望此属性使列表能够保留在其他控件的前面,并且永远不会被隐藏。 However, the meaning appeared to be quite different: 'keep on screen' means the screen always turn on. 但是,含义似乎有很大不同:“保留在屏幕上”表示屏幕始终处于打开状态。

Thanks to everybody for trying help 感谢大家的帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM