-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;
}
}
您可以從您的應用程序啓動此服務