问题描述
我想在屏幕关闭后显示该活动,在该活动上,我显示模拟时钟/数字时钟,但我有一个问题,就是调用广播接收器,但在屏幕关闭后却没有活动。我使用以下方法
- 前台服务
- 广播广播接收器
- SCREEN_ON和SCREEN_OFF用于检测屏幕状态。
// manifest file having
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="androidtutorial.project.nightclock">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.SET_ALARM" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-feature
android:name="android.software.live_wallpaper"
android:required="true" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true">
<activity android:name=".Activities.Setting" />
<activity android:name=".Activities.SetLockScreen" />
<activity android:name=".Activities.SetWallpaper" />
<activity android:name=".Activities.AddAlarm" />
<activity
android:name=".Activities.Watch"
android:theme="@style/Theme.AppCompat.NoActionBar" />
<activity android:name=".Activities.CountryList">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".Classes.AnalogClockWallPaperService"
android:enabled="true"
android:label="@string/app_name"
android:permission="android.permission.BIND_WALLPAPER">
<intent-filter>
<action android:name="android.service.wallpaper.WallpaperService" />
</intent-filter>
<Meta-data
android:name="android.service.wallpaper"
android:resource="@xml/clock_wallpaper" />
</service>
<service
android:name=".Services.OffScreenService"
android:enabled="true" />
<receiver
android:name=".broadCastRecivers.OffScreenbroadCast"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.SCREEN_OFF"/>
<action android:name="android.intent.action.SCREEN_ON"/>
</intent-filter>
</receiver>
<receiver
android:name=".broadCastRecivers.AlarmAlert"
android:enabled="true"
android:exported="true" />
</application>
</manifest>
//此处是自定义广播接收器
package androidtutorial.project.nightclock.broadCastRecivers;
import android.content.broadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
import androidtutorial.project.nightclock.Activities.AlwayOndisplay;
import androidtutorial.project.nightclock.Fragments.LockScreenPreview;
public class OffScreenbroadCast extends broadcastReceiver {
public static boolean wasScreenOn = true;
@Override
public void onReceive(Context context,Intent intent) {
// context.stopService(intent);
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Log.d("StackOverflow","Screen Off");
Intent intent1 = new Intent(context,AlwayOndisplay.class);// here i want to start activity
context.startActivity(intent1);
wasScreenOn = false;
Toast.makeText(context,"broadCast",Toast.LENGTH_LONG).show();
} else {
Log.d("StackOverflow","Screen ON");
wasScreenOn = true;
}
context.startService(intent);
}
}
//前台服务
package androidtutorial.project.nightclock.Services;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.notificationmanager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.broadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Build;
import android.os.IBinder;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;
import androidx.core.app.notificationmanagerCompat;
import androidtutorial.project.nightclock.broadCastRecivers.OffScreenbroadCast;
import androidtutorial.project.nightclock.Fragments.LockScreen;
import androidtutorial.project.nightclock.R;
public class OffScreenService extends Service {
public static final String START_FOREGROUND_SERVICE = "START_FOREGROUND_SERVICE";
public static final String STOP_FOREGROUND_SERVICE = "STOP_FOREGROUND_SERVICE";
private broadcastReceiver sReceiver;
@Override
public void onCreate() {
super.onCreate();
// registered recivers that handle the screen of and on
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_SCREEN_ON);
intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
sReceiver = new OffScreenbroadCast();
registerReceiver(sReceiver,intentFilter);
Toast.makeText(this,"recver registered",Toast.LENGTH_LONG).show();
}
public int onStartCommand(Intent intent,int flags,int startId) {
// here is code for always on display
if (intent != null) {
String action = intent.getAction();
if (action != null) {
switch (action) {
case START_FOREGROUND_SERVICE:
startForegroundService();
//Toast.makeText(getApplicationContext(),"Foreground service is started.",Toast.LENGTH_LONG).show();
break;
case STOP_FOREGROUND_SERVICE:
stopForegroundService();
break;
}
}
}
return START_STICKY;
}
// foreground service start here
private void startForegroundService() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createNotificationChannel();
} else {
Intent intent = new Intent();
PendingIntent pendingIntent = PendingIntent.getActivity(this,intent,0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
Notification notification = builder.setongoing(true)
.setContentTitle("Night Clock are display")
.setPriority(Notification.PRIORITY_MAX).setCategory(Notification.CATEGORY_SERVICE)
.setSmallIcon(R.drawable.lockscreen_icon)
.setContentIntent(pendingIntent).build();
notificationmanagerCompat notificationmanagerCompat = notificationmanagerCompat.from(this);
notificationmanagerCompat.notify(1,builder.build());
startForeground(1,notification);
}
}
// foreground service stop here
private void stopForegroundService() {
stopForeground(true);
stopSelf();
Toast.makeText(this,"service has been stopped",Toast.LENGTH_LONG).show();
}
// create notification channel
@RequiresApi(api = Build.VERSION_CODES.O)
private void createNotificationChannel() {
Intent resultIntent = new Intent(this,LockScreen.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,resultIntent,0);
String channelId = "MyService";
String channelName = "Foreground_service";
NotificationChannel channel = new NotificationChannel(channelId,channelName,notificationmanager.IMPORTANCE_DEFAULT);
channel.setLightColor(Color.BLUE);
channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
notificationmanager notificationmanager = (notificationmanager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationmanager.createNotificationChannel(channel);
Context context;
NotificationCompat.Builder builder = new NotificationCompat.Builder(this,channelId);
builder.setPriority(Notification.PRIORITY_MAX);
Notification notification = builder.setongoing(true)
.setContentTitle("Night Clock are display")
.setPriority(notificationmanager.IMPORTANCE_HIGH).setCategory(Notification.CATEGORY_SERVICE)
.setSmallIcon(R.drawable.lockscreen_icon)
.setContentIntent(pendingIntent).build();
notificationmanagerCompat notificationmanagerCompat = notificationmanagerCompat.from(this);
notificationmanagerCompat.notify(1,builder.build());
startForeground(1,notification);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
if (sReceiver != null)
unregisterReceiver(sReceiver);
super.onDestroy();
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)