gpt4 book ai didi

android - 如何从服务转到主屏幕?

转载 作者:行者123 更新时间:2023-11-30 01:04:56 25 4
gpt4 key购买 nike

我有这个 MainService,一旦启动,它会关闭屏幕并仅在计时器结束后打开它(此外,所述服务也会在计时器结束后停止)。

我的问题是:有没有办法让应用程序也进入后台,就在屏幕关闭之前?进入后台是指出现主屏幕。

主要 Activity :

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final Button startApp = (Button) findViewById(R.id.startApp);
final EditText timer = (EditText) findViewById(R.id.insertTimer);

assert startApp != null;
startApp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Countdown, Started", Toast.LENGTH_SHORT).show();

Intent intent = new Intent(MainActivity.this, MainService.class);

assert timer != null;
intent.putExtra("timer", timer.getText().toString());

Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 1000);

registerReceiver(broadcastReceiver, new IntentFilter(MainService.BROADCAST_ACTION));

startService(intent);
}
});
}

private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
updateUI(intent);
}
};

private void updateUI(Intent intent) {
String counter = intent.getStringExtra("counter");
TextView txtCounter = (TextView) findViewById(R.id.txtCounter);
assert txtCounter != null;
txtCounter.setText(counter);
}

主要服务:

public class MainService extends Service {

public static final String BROADCAST_ACTION = "com.example.vladpintea.friendsbeforecents.displayevent";
private final Handler handler = new Handler();
int counterr = 0;
Intent intentt;

String usedTimer;
long interval;

//TimerTask that will cause the run() runnable to happen.
TimerTask myTask = new TimerTask() {
public void run() {
stopSelf();
}
};
//Timer that will make the runnable run.
Timer myTimer = new Timer();

@Override
public void onCreate() {
intentt = new Intent(BROADCAST_ACTION);

registerReceiver(counter, new IntentFilter(Intent.ACTION_SCREEN_ON));

Toast.makeText(MainService.this, "Service, Created", Toast.LENGTH_SHORT).show();
}

private BroadcastReceiver counter = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(MainService.this, "Whoops! You've Lost.", Toast.LENGTH_SHORT).show();
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 30000);
}
};

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(MainService.this, "Service, Started", Toast.LENGTH_SHORT).show();

usedTimer = intent.getStringExtra("timer");
try {
interval = Long.parseLong(usedTimer);
} catch (NumberFormatException ignored) {}

myTimer.schedule(myTask, interval);

handler.removeCallbacks(sendUpdatesToUI);
handler.postDelayed(sendUpdatesToUI, 1000);

return super.onStartCommand(intent, flags, startId);
}

private Runnable sendUpdatesToUI = new Runnable() {
public void run() {
handler.postDelayed(this, 1000);
}
};

public void DisplayLoggingInfoPlus() {
intentt.putExtra("counter", String.valueOf(++counterr));
sendBroadcast(intentt);
}

@Override
public void onDestroy() {
DisplayLoggingInfoPlus();

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP
| PowerManager.ON_AFTER_RELEASE, "MyWakeLock");
wakeLock.acquire();

Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 30000);
}

@Override
public IBinder onBind(Intent intent) {
return null;
}

最佳答案

使用this post查看如何触发手动按下的 home。现在只需在您希望此行为发生的任何地方触发此代码,例如在锁定屏幕的计时器中。

关于android - 如何从服务转到主屏幕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38960923/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com