【发布时间】:2017-11-22 07:25:10
【问题描述】:
我有两个服务“home”和“alert”。家庭服务扫描附近的蓝牙设备以查找数字(1、2 或 3)。警报服务会扫描蓝牙以查找 0。
当扫描到 1、2 或 3 时,主服务会停止,并会启动新的活动,具体取决于数字。警报服务已启动。
当读取到 0 时,警报服务停止并将用户返回到主要活动。 home 服务重启。
我的问题,每次都会发生,是当我尝试在手机被锁定后第二次启动警报时,活动将无法启动。请参阅下面的列表,以便您进一步了解。
1- Home Service 和 mainActivity 在手机锁定的情况下在后台运行
2- 蓝牙读入 1
3- Home Service 已停止,Alert Service 已启动,Alert Screen 1 活动已启动
4- 警报服务读取 0
5- 警报服务已停止,主页服务已启动,mainActivity 活动已启动
6- Home Service 读取 1
7- 主页服务停止,警报屏幕 1 活动不会启动
如果我在锁定屏幕上启动活动后解锁手机,手机也不会启动活动。之后我可以启动警报屏幕活动 2 或 3,但不能启动警报屏幕 1。
如果我在解锁屏幕的情况下运行我的应用程序,一切正常。
Alerts Screen 1 Activity 的 onCreate
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON|
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED|
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
setContentView(R.layout.activity_yellow_flash);
首页服务 onStartCommand 该服务已启动,但线程完成所有工作。当线程扫描值为 1 时,我调用一个方法来启动一个新活动。这个方法最后一行中断了线程,进而停止了服务。
@Override
public int onStartCommand(Intent intent, int flags, int startID){
Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
Intent notifyIntent = new Intent(this, MainActivity.class);
PendingIntent PI = PendingIntent.getActivity(this, 0, notifyIntent, 0);
Notification notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.notify_lockout)
.setContentTitle("Lockout Application")
.setContentText("Running Services...")
.setContentIntent(PI).build();
Random random = new Random();
int notificationID = random.nextInt(9999 - 1000) + 1000;
startForeground(notificationID, notification);
// Start Thread
thread = new Thread(new Threader(startID));
thread.start();
return START_STICKY;
}
线程调用startActivity()方法,如下所示:
startAssistanceAlertActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startAssistanceAlertActivity);
Log.e("Test", "Called startActivity");
在启动活动失败期间,测试日志在 logcat 中可见。所以,我只是想知道为什么活动会开始一次,但永远不会再开始,不管我解锁手机。
对此有什么想法吗?如果您想查看一些特定的代码,请告诉我。
【问题讨论】:
-
Home Service and mainActivity are running in background with phone locked注意:活动不在后台运行。 -
我想我想说的是服务在屏幕锁定的情况下运行。
-
您可能会尝试在每次服务读取您的值时清除活动堆栈,例如:
Alert Service reads in a 0-> clearAlert Screen 1 -
@Ibrahim 我在从警报屏幕返回时使用了“Intent.FLAG_ACTIVITY_CLEAR_TOP”,但它不起作用。但是,另外将该标志添加到已修复的警报屏幕中。我知道它会是这么简单,我试图修复它好几天!非常感谢!
-
不客气,您需要收到它作为答案吗?帮助 OP?
标签: android android-activity android-service