简体   繁体   中英

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. 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. 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. WakeLock class is not used .

AndroidManifest.xml is following:

<?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:

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.

I've cleaned almost everything in the project: the application restored sleeping ability after deleting android:keepScreenOn attribute in ListView definition:

<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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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