moveTaskToBack(true);退到后台后再打开app无效,app继续走启动的activity走

1,设置去监听后退的按键方法
 @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
        boolean taskRoot = isTaskRoot();
        Log.d("myapp", taskRoot + "");
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() ==KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(getApplicationContext(), "再按一次退出到后台", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
//                finish();
//                System.exit(0);
                appContext.isbackup=true;//用个全局变量设置个标志,第二步去使用
                boolean b = moveTaskToBack(true);//退出到后台,这个方法可能会失效
                Log.d("myapp",b+"");
//                Intent intent = new Intent(Intent.ACTION_MAIN);
//                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                intent.addCategory(Intent.CATEGORY_HOME);
//                startActivity(intent);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

2,再App的启动页面

       boolean taskRoot = isTaskRoot();
//        if (!this.isTaskRoot()) {//这个方法再某些手机上可能无效,永远返回ture,把这个方法用个变量代替,用下面一行的全局变量代替
        if (application.isbackup == true) {
            Intent mainIntent = getIntent();
            String action = mainIntent.getAction();
            if (mainIntent.hasCategory(Intent.CATEGORY_LAUNCHER) && Intent.ACTION_MAIN.equals(action)) {
                finish();
                return;
            }
        }
//        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在安卓应用程序中,可以通过使用后台服务(Background Service)来实现让应用切换到后台继续执行某些操作的功能。具体步骤如下: 1. 创建一个后台服务类,继承自Service类,并在其中实现需要后台执行的操作。 2. 在应用程序的主Activity中,启动后台服务。 3. 在后台服务中,使用startForeground方法将服务放到前台,并在通知栏上显示一个通知,这样系统就会认为你的应用程序正在前台运行,不会轻易杀死它。同时,在服务结束运行时,需要调用stopForeground方法将其移出前台。 4. 在AndroidManifest.xml文件中声明你的后台服务。 下面是一个简单的后台服务的代码示例: ```java public class MyService extends Service { private static final String TAG = "MyService"; @Override public void onCreate() { super.onCreate(); Log.i(TAG, "onCreate"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.i(TAG, "onStartCommand"); // 在此处编写需要后台执行的代码 // 将服务放到前台并显示通知 Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.drawable.ic_launcher_foreground) .setContentTitle(getText(R.string.notification_title)) .setContentText(getText(R.string.notification_message)) .setPriority(NotificationCompat.PRIORITY_LOW) .build(); startForeground(1, notification); return START_STICKY; } @Override public void onDestroy() { super.onDestroy(); Log.i(TAG, "onDestroy"); // 将服务从前台移除 stopForeground(true); } @Nullable @Override public IBinder onBind(Intent intent) { return null; } } ``` 需要注意的是,后台服务不应该长时间占用系统资源,否则会严重影响手机的性能和电池寿命。在应用程序退出时,应该及时停止后台服务。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值