國內(nèi)廠商定制,除非廠商給app白名單,否則只能用戶手動(dòng)添加白名單(應(yīng)用自啟和后臺(tái)運(yùn)行),才能通過前臺(tái)服務(wù)實(shí)現(xiàn)app?;?。
這里介紹前臺(tái)服務(wù)相關(guān)實(shí)現(xiàn)方式。
開啟服務(wù):
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//安卓8.0以上開啟為前臺(tái)服務(wù)
startForegroundService(new Intent(this, KeepAliveNotificationService.class));
} else {
startService(new Intent(this, KeepAliveNotificationService.class));
}
服務(wù):文章來源:http://www.zghlxwxcb.cn/news/detail-702314.html
public class KeepAliveNotificationService extends Service {
private final String CHANNEL_ONE_ID = "100";
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
//創(chuàng)建通知欄常駐通知
initNotification();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// return super.onStartCommand(intent, flags, startId);
//返回START_STICKY,被系統(tǒng)或手動(dòng)清理后可重啟
return START_STICKY;
}
@Override
public void onDestroy() {
stopForeground(true);
}
/**
* 開啟通知欄
*/
private void initNotification() {
//獲取管理器
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//創(chuàng)建點(diǎn)擊跳轉(zhuǎn)Activity
Intent intent = new Intent(this, NotificationActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
//創(chuàng)建notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ONE_ID)
.setContentIntent(pendingIntent) // 設(shè)置PendingIntent
.setSmallIcon(R.mipmap.user_notification_ic_launcher) // 設(shè)置狀態(tài)欄內(nèi)的小圖標(biāo)
//.setLargeIcon(bitmapIcon)// 設(shè)置大圖標(biāo)
.setContentTitle("推送服務(wù)")
.setContentText("應(yīng)用更好的接收推送服務(wù)") // 設(shè)置內(nèi)容
.setWhen(System.currentTimeMillis())// 設(shè)置該通知發(fā)生的時(shí)間
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)// 鎖屏顯示全部通知
//.setDefaults(Notification.DEFAULT_ALL)// //使用默認(rèn)的聲音、振動(dòng)、閃光
.setCategory(Notification.CATEGORY_SERVICE)//設(shè)置類別
.setPriority(NotificationCompat.PRIORITY_MAX);// 優(yōu)先級(jí)為:重要通知
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//安卓8.0以上系統(tǒng)要求通知設(shè)置Channel,否則會(huì)報(bào)錯(cuò)
NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ONE_ID, "服務(wù)常駐通知", NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);//鎖屏顯示全部通知
manager.createNotificationChannel(notificationChannel);
builder.setChannelId(CHANNEL_ONE_ID);
}
Notification notification = builder.build(); // 獲取構(gòu)建好的Notification
//notification.defaults = Notification.DEFAULT_SOUND; //設(shè)置為默認(rèn)的聲音
notification.flags = Notification.FLAG_NO_CLEAR;//不消失的常駐通知
startForeground(1, notification);//設(shè)置常駐通知
}
}
清單文件文章來源地址http://www.zghlxwxcb.cn/news/detail-702314.html
<!-- 自定義 前臺(tái)服務(wù) -->
<service
android:name="com.xx.xxxx.service.KeepAliveNotificationService"
android:directBootAware="true"
android:enabled="true"
android:exported="true"
android:foregroundServiceType="phoneCall|mediaPlayback|dataSync|mediaProjection|connectedDevice|location"
android:label="@string/app_name" />
到了這里,關(guān)于Android app?;睿ㄇ芭_(tái)服務(wù))的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!