Android 锁屏后Service服务保活(支持9.0)及锁屏后弹activity

service保活

问题:
后台Service启动正常启动后,锁屏状态下大概80秒左右Service就被暂停了(并没有被杀死),唤醒屏幕后就继续执行。

解决思路:
Service启动时创建一条通知,与其绑定,这样锁屏或者后台Service都不会被暂停或杀死。

代码如下:
在Service的onCreate中创建NotificationChannel 并且与服务绑定。    

@Override
public void onCreate() {
    super.onCreate();

    Notification.Builder builder = new Notification.Builder(this)
            .setSmallIcon(R.mipmap.notify)
            .setContentTitle("title")
            .setContentText("text");

    //创建NotificationChannel
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        String notificationId = "TgStartServiceId";
        String notificationName = "TgStartServiceName";
        NotificationChannel channel = new NotificationChannel(notificationId, notificationName, NotificationManager.IMPORTANCE_HIGH);
        ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);

        builder.setChannelId(notificationId);
    }

    Notification notification = builder.build();

    startForeground(1, notification);
}

开启service:

需要根据版本判断是调用startForegroundService()或者startService()函数;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //android8.0以上通过startForegroundService启动service
            startForegroundService(new Intent(MainActivity.this, MyService.class));
        }else{
            startService(new Intent(MainActivity.this, MyService.class));
        }
    }
}

锁屏后弹activity

<uses-permission android:name="android.permission.REORDER_TASKS" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
  • 首先,在需要息屏弹出的Activity上我们需要添加如下代码:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.e("弹出界面", "onCreate1");
        super.onCreate(savedInstanceState);
        //锁屏后也能弹出
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON //保持屏幕常亮
                | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED  //在锁屏情况下也可以显示屏幕
                | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);  //启动Activity的时候点亮屏幕
        setContentView(R.layout.activity_pop);  
    ................
    }
  • 其次:在启动Activity的代码中需要通过PowerManager服务获取必要能力来进行启动,添加如下代码

KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
final KeyguardManager.KeyguardLock kl = km.newKeyguardLock("unLock");
//解锁软盘
kl.disableKeyguard();
//获取电源管理器对象

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
//获取PowerManager.WakeLock对象,后面的参数|表示同时传入两个值,最后的是LogCat里用的Tag
final PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK, "bright");
//点亮屏幕
wl.acquire();

Intent intent = new Intent(this, PopActivity.class);
intent.putExtra(PopActivity.POP_ACTIVITY_NAME, stocks.get(0).getName());
intent.putExtra(PopActivity.POP_ACTIVITY_CODE, stocks.get(0).getCode());
startActivity(intent);

//释放
wl.release();
kl.reenableKeyguard();
  • 代码解析:

    • Wake Lock是一种锁的机制, 只要有人拿着这个锁,系统就无法进入休眠, 可以被用户态程序和内核获得. 这个锁可以是有超时的 或者 是没有超时的, 超时的锁会在时间过去以后自动解锁。如果没有锁了或者超时了, 内核就会启动休眠的那套机制来进入休眠.

    • PowerManager.WakeLock 有加锁和解锁两种状态,加锁的方式有两种: 第一种是永久的锁住,这样的锁除非显式的放开,否则是不会解锁的,所以这种锁用起来要非常的小心。 第二种锁是超时锁,这种锁会在锁住后一段时间解锁。

    • 在创建了 PowerManager.WakeLock 后,有两种机制,第一种是不计数锁机制,另一种是计数锁机制。可以通过 setReferenceCounted(boolean value) 来指定,一般默认为计数机制。这两种机制的区别在于,前者无论 acquire() 了多少次,只要通过一次 release()即可解锁。而后者正真解锁是在( --count == 0 )的时候,同样当 (count == 0) 的时候才会去申请加锁。所以 PowerManager.WakeLock 的计数机制并不是正真意义上的对每次请求进行申请/释放每一把锁,它只是对同一把锁被申请/释放的次数进行了统计,然后再去操作。

    • 我们上面代码仅使用默认的计数锁机制,一个acquire对应一个release

  • flags参数说明:

    • PARTIAL_WAKE_LOCK :保持CPU 运转,屏幕和键盘灯是关闭的。

    • SCREEN_DIM_WAKE_LOCK :保持CPU 运转,允许保持屏幕显示但有可能是灰的,关闭键盘灯

    • SCREEN_BRIGHT_WAKE_LOCK :保持CPU 运转,保持屏幕高亮显示,关闭键盘灯

    • FULL_WAKE_LOCK :保持CPU 运转,保持屏幕高亮显示,键盘灯也保持亮度

    • ACQUIRE_CAUSES_WAKEUP: 一旦有请求锁时,强制打开Screen和keyboard light

    • ON_AFTER_RELEASE: 在释放锁时reset activity timer

补充知识:

后台执行限制

Android 8.0 为提高电池续航时间而引入的变更之一是,当您的应用进入已缓存状态时,如果没有活动的组件,系统将解除应用具有的所有>>>唤醒锁。

如果针对 Android 8.0 的应用尝试在不允许其创建后台服务的情况下使用 startService() 函数,则该函数将引发一个 IllegalStateException。
新的 Context.startForegroundService() 函数将启动一个前台服务。现在,即使应用在后台运行,系统也允许其调用 Context.startForegroundService()。不过,应用必须在创建服务后的五秒内调用该服务的 startForeground() 函数。

因此我们需要根据版本判断是调用startForegroundService()或者startService()函数;

因此我们必须在服务创建之后调用startForeground()函数。


在android8.0中对Notification进行了变更:

Android 8.0 中的通知长按菜单。

通知渠道:Android 8.0 引入了通知渠道,其允许您为要显示的每种通知类型创建用户可自定义的渠道。用户界面将
通知渠道称之为通知类别。要了解如何实现通知渠道的信息,请参阅通知渠道指南。

通知标志:Android 8.0 引入了对在应用启动器图标上显示通知标志的支持。通知标志可反映某个应用是否存在与其
关联、并且用户尚未予以清除也未对其采取行动的通知。通知标志也称为通知点。要了解如何调整通知标志,请参阅通知标志指南。

休眠:用户可以将通知置于休眠状态,以便稍后重新显示它。重新显示时通知的重要程度与首次显示时相同。应用可以移除或更新已休眠的通知,但更新休眠的通知并不会使其重新显示。

通知超时:现在,使用 setTimeoutAfter() 创建通知时您可以设置超时。您可以使用此函数指定一个持续时间,超过该持续时间后,通知应取消。如果需要,您可以在指定的超时持续时间之前取消通知。

通知设置:当您使用 Notification.INTENT_CATEGORY_NOTIFICATION_PREFERENCESIntent 从通知创建指向应用通知设置的链接时,您可以调用 setSettingsText() 来设置要显示的文本。此系统可以提供以下 Extra 数据和 Intent,用于过滤应用必须向用户显示的设置:EXTRA_CHANNEL_ID、NOTIFICATION_TAG 和 NOTIFICATION_ID。

通知清除:系统现在可区分通知是由用户清除,还是由应用移除。要查看清除通知的方式,您应实现 NotificationListenerService 类的新 onNotificationRemoved() 函数。

背景颜色:您现在可以设置和启用通知的背景颜色。只能在用户必须一眼就能看到的持续任务的通知中使用此功能。例如,您可以为与驾车路线或正在进行的通话有关的通知设置背景颜色。您还可以使用 Notification.Builder.setColor() 设置所需的背景颜色。这样做将允许您使用 Notification.Builder.setColorized() 启用通知的背景颜色设置。

消息样式:现在,使用 MessagingStyle 类的通知可在其折叠形式中显示更多内容。对于与消息有关的通知,您应使用 MessagingStyle 类。您还可以使用新的 addHistoricMessage() 函数,通过向与消息相关的通知添加历史消息为会话提供上下文。

同时提供了示例代码:android-NotificationChannels

因此我们需要针对Android8.0以上版本增加NotificationChannel,如下所示:

public class MyService extends Service {

    private NotificationManager notificationManager;
    private String notificationId = "channelId";
    private String notificationName = "channelName";

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private Notification getNotification() {
        Notification.Builder builder = new Notification.Builder(this)
                .setSmallIcon(R.drawable.logo_small)
                .setContentTitle("测试服务")
                .setContentText("我正在运行");
        //设置Notification的ChannelID,否则不能正常显示
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder.setChannelId(notificationId);
        }
        Notification notification = builder.build();
        return notification;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        //创建NotificationChannel
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            NotificationChannel channel = new NotificationChannel(notificationId, notificationName, NotificationManager.IMPORTANCE_HIGH);
            notificationManager.createNotificationChannel(channel);
        }
        startForeground(1,getNotification());
    }
}

————————————————
版权声明:本文为CSDN博主「苍蝇小黑」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/huaheshangxo/article/details/82856388

 

参考:

 

https://blog.csdn.net/qq_38356174/article/details/90520621

 

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Android开发中,我们可以通过设置WindowManager.LayoutParams的FLAG_SHOW_WHEN_LOCKED属性来实现在锁屏状态下显示Activity的功能。 首先,在Activity的onCreate()方法中,我们需要申请获取Window对象,并为其设置LayoutParams属性: ```java @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); WindowManager.LayoutParams attrs = getWindow().getAttributes(); attrs.flags |= WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED; getWindow().setAttributes(attrs); setContentView(R.layout.activity_main); // 其他初始化操作 } ``` 在上述代码中,我们通过getWindow().getAttributes()方法获取当前Activity的Window对象,并通过实例化WindowManager.LayoutParams对象获取其LayoutParams属性。然后,我们将attrs.flags属性的FLAG_SHOW_WHEN_LOCKED位设置为1,表示在锁屏状态下显示Activity。 接下来,我们使用getWindow().setAttributes(attrs)方法将新的LayoutParams属性应用于Window。 最后,我们使用setContentView(R.layout.activity_main)方法为Activity设置布局界面,将具体的界面展示出来。 需要注意的是,为了保证Activity锁屏状态下正常显示,我们还需要在AndroidManifest.xml文件中对Activity进行一些配置: ```xml <activity android:name=".MainActivity" android:showOnLockScreen="true" ... > <!-- 其他属性配置 --> </activity> ``` 在上述代码中,我们在<activity>标签中添加一个android:showOnLockScreen="true"属性,表示该Activity可以在锁屏状态下显示。 通过以上配置和代码,我们可以在Android应用中设置锁屏显示Activity,从而实现在锁屏状态下展示相关的界面信息。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值