-1
我想创建一个应用程序,当用户在手机处于睡眠模式时在主屏幕按钮中点击3次以上时,该应用程序将被激活。我如何在Android中实现这一点?如何创建Android应用程序,以响应在睡眠模式下手机的物理按钮的点击
我想创建一个应用程序,当用户在手机处于睡眠模式时在主屏幕按钮中点击3次以上时,该应用程序将被激活。我如何在Android中实现这一点?如何创建Android应用程序,以响应在睡眠模式下手机的物理按钮的点击
您需要创建一个后台服务。请注意,您还需要创建一个通知该服务时,这将是可见的运行:
public class MyApp extends Application {
@Override
public void onCreate() {
startService(new Intent(this, BgService.class));
}
}
:
public class BgService extends Service {
@Override
public int onStartCommand(Intent i, int flags, int startId) {
startForeground(C.MAIN_SERVICE_NOTIFICATION_ID, buildNotification(getString(R.string.service_title)));
return START_NOT_STICKY;
}
protected Notification buildNotification(String content) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setTicker(getString(R.string.app_name))
.setContentTitle(getString(R.string.app_name))
.setContentText(content)
.setSmallIcon(R.drawable.notification_icon)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.app_icon))
.setWhen(System.currentTimeMillis())
.setAutoCancel(false)
.setOngoing(true)
.setContentIntent(pendingIntent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
builder.setPriority(Notification.PRIORITY_HIGH);
Notification notification = builder.build();
notification.flags |= Notification.FLAG_NO_CLEAR;
return notification;
}
}
您可以从您的应用程序启动此服务