简体   繁体   English

每次在android oreo上解锁设备时都会启动活动

[英]launch an activity every time the device is unlocked on android oreo

I am currently working on an app to launch an activity every time the device is unlocked, the following code works well for smaller devices with the Android api oreo. 我目前正在开发一个应用程序,以在每次解锁设备时启动一项活动,以下代码适用于装有Android api oreo的小型设备。

Manifest: 表现:

<receiver
        android:name=".ScreenReceiver"
        android:enabled="true" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.SCREEN_OFF" />
            <action android:name="android.intent.action.SCREEN_ON" />
            <action android:name="android.intent.action.USER_PRESENT" />
            <category android:name="android.intent.category.DEFAULT" >
            </category>
        </intent-filter>
    </receiver>

I also have in the manifest 我也有清单

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

ScreenReceiver class: ScreenReceiver类:

public class ScreenReceiver extends BroadcastReceiver {
ScreenReceiver screen;
Context context=null;
@Override
public void onReceive(Context context, Intent intent)
{
    this.context=context;
    if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)||intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)||intent.getAction().equals(Intent.ACTION_USER_PRESENT))
    {
        Intent i = new Intent(context, DictionaryView.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }
}

} }

I need to launch an activity every time the phone is unlock the previous code works pretty well for earlier versions of Android Oreo, but this version does not work because it never starts the activity after unlocking the device. 每当手机解锁时,我都需要启动一个活动,以前的代码对于早期版本的Android Oreo来说效果很好,但是此版本无法正常工作,因为它在解锁设备后永远不会启动活动。 Friends please I need help takes a couple of hours looking for useful information without success. 朋友们,我需要帮助,需要几个小时才能找到有用的信息,但没有成功。

You might need to ask for permission from the user 您可能需要征求用户的许可

if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.RECEIVE_BOOT_COMPLETED)  != PackageManager.PERMISSION_GRANTED
            )
    {
        //REQUEST RUNTIME PERMISSION
        ActivityCompat.requestPermissions(this, new String[] {
                android.Manifest.permission.RECEIVE_BOOT_COMPLETED,                    
        }  ,    MY_PERMISSION_REQUEST_CODE);
    }

Then you will have to setup an overrride method to handle on requestpermissionresult 然后,您将必须设置一个替代方法来处理requestpermissionresult

 @Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode)
    {
        case MY_PERMISSION_REQUEST_CODE:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
            {
               //do this
            }
    }
}

Try this: 尝试这个:

Your code look like this: 您的代码如下所示:

<receiver android:name=".ScreenReceiver"
    android:enabled="true" >
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <action android:name="android.intent.action.SCREEN_OFF" />
        <action android:name="android.intent.action.SCREEN_ON" />
        <action android:name="android.intent.action.USER_PRESENT" />
        <category android:name="android.intent.category.DEFAULT" >
        </category>
    </intent-filter>

Please remove all tag in intent filter code look like this 请删除意图过滤器代码中的所有标签,如下所示

<receiver android:name=".ScreenReceiver>
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
        </intent-filter>

ScreenReceiver class: ScreenReceiver类:

public class ScreenReceiver extends BroadcastReceiver {
ScreenReceiver screen;
Context context=null;
   @Override
    public void onReceive(Context context, Intent intent) {
    if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
        Intent i = new Intent(context, DictionaryView.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }
}
}

try this i hope it helps you. 试试这个,希望对您有帮助。

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

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