0

我想要做的是在用户退出我的应用程序时向我的后端服务器发送一个注销请求(可能在任何活动期间发生,有很多)。

似乎我无法使用OnStop(),因为我有一个画廊选择器和相机 Intent 以及它们何时启动OnPause并被OnStop调用。我需要一种方法来明确知道该应用程序已关闭。

我已经阅读了有关使用Service/BroadcastReceiver甚至是LocalBroadcastManager,或者可能将请求绑定到单击主页按钮的信息。

我无法检查应用程序是否已发送到后台,因为这对于相机/图库 Intent 启动以及正在发送到后台的应用程序都是如此。我还尝试检查正在启动的活动的包名称,但这可能在其他设备上是可变的(例如,画廊可能有不同的包名称)。

非常感谢任何建议/方向。

编辑:我发现实际上没有办法拦截按下主页按钮。仍在寻找解决方案!

4

1 回答 1

1

我有一个类似的问题,我使用服务来解决我的问题。这就是我所做的

在主要活动中

 ServiceConnection mConnection = new ServiceConnection() {
 public void onServiceConnected(ComponentName className,
                                       IBinder binder) {
            ((KillingNotificationBar.KillBinder) binder).service.startService(new Intent(
                    Main.this, KillingNotificationBar.class));
        }

        public void onServiceDisconnected(ComponentName className) {
        }

    };
 bindService(new Intent(Main.this,
                    KillingNotificationBar.class), mConnection,
            Context.BIND_AUTO_CREATE);

KillingNotificationBar 类

public class KillingNotificationBar extends Service {
private final IBinder mBinder = new KillBinder(this);
public class KillBinder extends Binder {
    public final Service service;
    public KillBinder(Service service) {
        this.service = service;
    }
}
@Override
public IBinder onBind(Intent intent) {
    return mBinder;
}

@Override
public void onCreate() {
    you will know if the activity is destroyed
}
}

将此添加到您的清单中

 <service android:name=".services.KillingNotificationBar"/>

注意 执行需要 1-5 秒。

于 2015-07-24T05:29:35.510 回答