public class LockScreenService extends Service {
public static final int LOCK_SCREEN_SERVICE_NOTIFICATION_ID = 1;
public static final int SERVICE_NOTIFICATION_PENDING_INTENT_REQUEST_CODE = -10;
private BroadcastReceiver screenOnReceiver;
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "start", Toast.LENGTH_SHORT).show();
localeSet(this);
startForeground(LOCK_SCREEN_SERVICE_NOTIFICATION_ID, createServiceNotification());
screenOnReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "screen on!", Toast.LENGTH_SHORT).show();
}
};
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_SCREEN_ON);
registerReceiver(screenOnReceiver, intentFilter);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
if (screenOnReceiver != null) {
unregisterReceiver(screenOnReceiver);
screenOnReceiver = null;
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
private Notification createServiceNotification() {
Intent notificationIntent = new Intent(this, SplashActivity.class); // activity you wanna show when the notification is clicked
int flags = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S ? PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE : PendingIntent.FLAG_UPDATE_CURRENT;
PendingIntent pendingIntent = PendingIntent.getActivity(this, SERVICE_NOTIFICATION_PENDING_INTENT_REQUEST_CODE, notificationIntent, flags);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, getResources().getString(R.string.service_notification_channel_id))
.setSmallIcon(R.drawable.ic_stat_notification_icon)
.setContentTitle(getResources().getString(R.string.service_notification_title))
.setContentText(getResources().getString(R.string.service_notification_content))
.setContentIntent(pendingIntent)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
return builder.build();
}
private void localeSet(Context context) {
SharedPreferences prefs = context.getSharedPreferences("AppSettings", Context.MODE_PRIVATE);
String langCode = prefs.getString("languageCode", "");
Locale locale = new Locale(langCode);
Locale.setDefault(locale);
Configuration config = context.getResources().getConfiguration();
config.setLocale(locale);
context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
}
嗨!我是一名新手程序员,有很多东西需要学习。 我正在尝试制作一个 Android 应用程序,它是在锁定屏幕上显示用户待办事项列表的待办事项应用程序。
我制作了一个前台服务,其中有一个广播接收器实例作为字段,并且广播接收器监听“屏幕打开”操作,因此当用户打开屏幕时,应用程序会向用户显示一个活动。正如您在上面的代码中所看到的,目前,当屏幕打开时,应用程序会向您显示“屏幕打开!”看起来效果很好。
screenOnReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_SCREEN_ON.equals(intent.getAction())) {
Toast.makeText(context, "screen on", Toast.LENGTH_SHORT).show();
// // Intent to launch the desired activity
Intent activityIntent = new Intent(context, LockScreenActivity.class);
//
// // Include these flags to launch the activity even if the device is locked
activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
//
// // Starting the activity
startActivity(activityIntent);
}
}
};
然后我以这种方式更改了广播接收器代码,它有点在锁定屏幕上打开了一个活动。但是当我关闭应用程序时,即使前台服务处于活动状态,应用程序也没有打开活动。它只显示“屏幕打开!”吐司。即使应用程序已关闭,我应该如何从服务中弹出活动?
我一直在寻找这个,但他们没有关于这个的最新信息。 我也读过 https://developer.android.com/guide/components/activities/background-starts 我让应用程序向用户询问 SYSTEN_ALERT_WINDOW 权限,但即使获得了权限,当应用程序关闭时,应用程序也没有在锁定屏幕上显示活动。
※ 抱歉我的英语不好,我的母语不是英语。
必须在服务内的
PendingIntent
上添加 Notification
:
/** The content {@link Intent}, which leads to {@link MainActivity}. */
@NonNull
private PendingIntent getContentPendingIntent() {
Intent contentIntent = new Intent(this, MainActivity.class);
contentIntent.setData(Uri.parse("someapp://somedeeplink")); // optional.
return PendingIntent.getActivity(this, 0, contentIntent, this.getIntentFlags());
}
它的分配是这样的:
NotificationCompat.Builder builder = this.getNotificationBuilder();
builder.setContentIntent(this.getContentPendingIntent());