在某些分钟内启动服务以在屏幕上显示窗口,

时间:2018-08-15 12:26:37

标签: android service

//我是新手对于android,我不知道如何启动Service,仅说我想在10分钟内启动它,我想在屏幕上显示Window。请帮忙。

public class TopWindow extends Service {

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    WindowManager.LayoutParams p = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG,
            WindowManager.LayoutParams.FLAG_FULLSCREEN |   WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT
    );
    // Define the position of the window within the screen
    p.gravity = Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL;
    p.x = 0;
    p.y = 100;
    final WindowManager windowManager = (WindowManager)getApplicationContext().getSystemService(WINDOW_SERVICE);
    LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
    final View popupview = inflater.inflate(R.layout.custom_window_layout,null);
    windowManager.addView(popupview,p);
    return super.onStartCommand(intent, flags, startId);

}

1 个答案:

答案 0 :(得分:0)

尝试一下:-

private AlarmManager alarmMgr;
private PendingIntent alarmIntent;

alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmService.class);
alarmIntent = PendingIntent.getService(context, 0, intent, 0);
alarmMgr.set(AlarmManager.RTC_WAKEUP,System.currentTimeMillis() +60 * 1000 *10, alarmIntent);

More detail on how to set alarms in android .

确保已将此清单包含在服务标签内的清单中:-

 <service android:enabled="true" android:name=".ServiceClass" />