Android onTaskRemoved() 调用 webservice

2023-12-15

美好的一天。我遇到了可怕的情况。我正在会话中创建位置共享逻辑。我将该会话保留在 mysql 上的服务器上。当 android 命中该活动时,我会插入相应的用户信息。当 android 离开该活动时,我当然会删除该信息列,因此会话被另一侧放弃。一切都很好,直到出现一个问题。Android 不为从最近使用的应用程序中滑动的应用程序提供回调,这意味着应用程序被完全杀死。我在那里找到了一个解决方案。我正在使用一项服务并且一旦达到我想要的活动就启动服务。在服务中,我有一个名为 onTaskRemoved() 的简单东西,一旦应用程序通过从最近的应用程序中滑动而被杀死,它就会通知我。一切都很好,直到我想调用我的服务器为了删除该列。调用不会直接通过,我永远不会在那里收到任何响应,但在 onDestroy() 中一切都按预期工作。实际上这里是代码

 @Override
public void onTaskRemoved(Intent rootIntent) {
    destroySession();
    super.onTaskRemoved(rootIntent);
}

private void destroySession() {
    Log.d("Fsafasfsafasfas", "destroySession: " + opponentId + " my user id" + sharedHelper.getUserId());
    Call<ResponseBody> locationCall = Retrofit.getInstance().getInkService().requestFriendLocation(sharedHelper.getUserId(), opponentId, "", "", Constants.LOCATION_REQUEST_TYPE_DELETE);
    locationCall.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            if (response == null) {
                destroySession();
                return;
            }
            if (response.body() == null) {
                destroySession();
                return;
            }
            try {
                String responseBody = response.body().string();
                Log.d("Fsafasfsafasfas", "onResponse: " + responseBody);
                JSONObject jsonObject = new JSONObject(responseBody);
                boolean success = jsonObject.optBoolean("success");
                if (success) {
                    stopSelf();
                } else {
                    destroySession();
                }
            } catch (IOException e) {
                stopSelf();
                e.printStackTrace();
            } catch (JSONException e) {
                stopSelf();
                e.printStackTrace();
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            destroySession();
        }
    });
}

我猜这个电话永远不会接通,因为打印的唯一日志就是 id 的日志,仅此而已。有人知道发生了什么事吗?我该如何处理这种情况?


/**
 * Created by Parag on 01/05/2017.
 */

public class AppService extends android.app.Service {
    public static final String TAG=AppService.class.getName();
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Bundle bundle=intent.getExtras();
        if(bundle!=null){
            final String logout=bundle.getString("logout");
            final String driverLoginId=bundle.getString("driverLoginId");
            if(logout!=null&&driverLoginId!=null){
                Toast.makeText(this, "logging out", Toast.LENGTH_SHORT).show();
                Log.e(TAG,"Logging out");
                Log.e(TAG,"Inside driverLogout "+driverLoginId);
                Call<LogoutResponse> call = RestHandler.getApiService().driverLogout(driverLoginId);
                call.enqueue(new Callback<LogoutResponse>() {
                    @Override
                    public void onResponse(Call<LogoutResponse> call, Response<LogoutResponse> response) {
                        //close the service on receiving response from API
                        Log.e("Response : ",response.body().getStatus()+"");
                        AppService.this.stopSelf();
                    }
                    @Override
                    public void onFailure(Call<LogoutResponse> call, Throwable t) {
                        //close the service on receiving response from API
                        AppService.this.stopSelf();
                    }
                });

            }else{
                //Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
                Log.e(TAG,"DriverLoginId : "+driverLoginId);
                Log.e(TAG,"Logout : "+logout);
            }
        }else{
            //Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
            Log.e(TAG,"Service Start");
        }
        return super.onStartCommand(intent,flags,startId);
    }

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

    @Override
    public void onTaskRemoved(Intent rootIntent) {

        Log.e(TAG,"Service Stop");
        UserLocalStore userLocalStore=new UserLocalStore(AppService.this);
        Log.e("USER DATA :",userLocalStore.fetchUserData().toString());
        Intent restartServiceTask = new Intent(getApplicationContext(),this.getClass());
        restartServiceTask.setPackage(getPackageName());
        restartServiceTask.putExtra("logout","true");
        restartServiceTask.putExtra("driverLoginId",userLocalStore.fetchUserData().getUserId());
        PendingIntent restartPendingIntent =PendingIntent.getService(getApplicationContext(), 1,restartServiceTask, PendingIntent.FLAG_ONE_SHOT);
        AlarmManager myAlarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
        myAlarmService.set(
                AlarmManager.ELAPSED_REALTIME,
                SystemClock.elapsedRealtime() + 1000,
                restartPendingIntent);
        super.onTaskRemoved(rootIntent);
    }


}

我也面临同样的问题,无法从 onTaskRemoved 方法进行任何 API 调用。 所以,我也研究了很多,但没有找到解决方案。所以,我终于有了重新启动 Web 服务的想法,并在意图中放置了一些额外的内容。通过这种方式,您可以区分服务何时重新启动以执行 API 调用。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android onTaskRemoved() 调用 webservice 的相关文章

  • Mono for Android,读取和写入 XLSX

    我正在使用 Mono for Android 开发一个应用程序 我需要读取和创建 XLSX Excel 文件的功能 我尝试过EPPlus和NPOI 并快速浏览了一下微软的Open XML SDK 发现了以下问题 EPPlus 需要 Wind
  • 无法解析符号“AndroidJUnit4”

    显然我需要正确的导入语句来解决这个问题 根据文档用于AndroidJUnit4 http developer android com reference android support test runner AndroidJUnit4 h
  • 如何随着 ViewPager 位置偏移量的变化对视图进行动画处理

    我们希望创建一个带有动画的应用程序介绍 用户可以在其中滚动页面 并且当用户滚动时 视图会动画化并遍历所有幻灯片 动画视图应该随着用户滚动而移动 因此如果用户滚动得更快 动画视图应该移动得更快 如果用户滚动回到上一页 动画视图应该向后移动 这
  • 在旧版本的 API 上更改 ContentObserver Onchange 上的 uri [重复]

    这个问题在这里已经有答案了 可能的重复 如何获取内容观察器中插入行的 URI https stackoverflow com questions 8432800 how to get uri of inserted row in my co
  • 按大小、日期、名称等在回收器视图中排序并记住选择

    我正在制作图库应用程序 我想为其添加排序功能 我可以使用以下命令在运行时对项目进行排序Comparator但问题是 每当我退出应用程序时 列表都会再次从数据库中出来 并且所有列表都未排序 我想在我的应用程序中提供按日期 大小 名称等排序的选
  • 使用选项卡式活动中的捆绑包将值从活动传递到片段

    我是一个java文盲 但仍在尝试开发一个供我个人使用的应用程序 我从 android studio 的 Tabbed Activity 开始 除了 MainActivity 中的一个片段和一个包之外 大部分没有改变 这是我的代码 主要活动
  • Android NumberPicker 带字符串

    I have customised the NumberPicker to show text The output is this 当我按 确定 时 我想将 e x 鼠标添加到我的列表 文章 中 我得到的是索引值 int 它由 array
  • Android TableRow 垂直拉伸以填充屏幕

    我正在尝试创建一个电话拨号器视图 使用 TableLayout 在 3x4 网格中创建 12 个按钮 我希望行垂直拉伸以平等地使用所有可用空间 但似乎 fill parent 在 TableRows 上不起作用 我不想使用 setMinim
  • 如何创建 Google Play 音乐的直接链接?

    据我所知 应用程序的直接链接是 gt market apps collection
  • AWS MobileHub:重命名 Android / iOS 示例项目

    我是 AWS Mobilehub 的新手 我喜欢它允许我使用 AWS 配置选项创建项目 但是 当我尝试构建应用程序 ios swift android 时 它总是使用我的示例项目作为项目名称 在 AWS 项目的大多数配置设置中 例如使用 c
  • 颤动附近的连接

    当我尝试在设备上做广告或发现时 我收到此错误 但是前一天在环路上效果很好 PlatformException Failure 17 API Nearby CONNECTIONS API is not available on this de
  • 如何增加颤振中切屑的宽度

    我想增加宽度Chip 我怎样才能实现这个目标 Chip elevation 6 0 backgroundColor Colors white shape RoundedRectangleBorder borderRadius BorderR
  • 为什么在回收器视图中滚动后值会消失?

    Data before scrolling Data after scrolling 我的应用程序的问题如上图所示 输入数据后 如果我在将项目添加为可滚动后滚动 数据就会消失 作为进一步的解释 有时输入的数据出现在已添加的其他项目中 为了解
  • Android - 如何合并两个视频

    基本上 我正在寻找一种将两个 mp4 视频文件 在 SD 卡上 组合在一起的方法 更像是在第一个视频的末尾附加第二个视频 我进行了很多搜索 但找不到合适的解决方案 好吧 我根本找不到任何解决方案 所以我的问题是 是否有一个库可以组合 并可能
  • DeadSystemException启动服务Android 7

    在过去的几周里 我在我的事故报告中看到 Fatal Exception java lang RuntimeException Unable to start service com MyService ef705d8 with Intent
  • 有没有办法模拟小部件或屏幕特定位置的触摸?

    我想触摸或点击小部件上的某处 而不让用户在此时明确触摸屏幕 有什么办法可以做到吗 我已经检查了SO答案 有些人建议使用 集成测试 但在未物理或以某种方式连接到笔记本电脑的设备上无法执行 集成测试 无法找到更好的措辞 我还尝试进行 hitTe
  • Kotlin 中是否有类似于 #region #endregion 的语法?

    我知道我可以使用 region endregion 包围 C 中的代码片段 Kotlin 中是否有类似的语法 谢谢 region MyRegion protected void Page Load object sender EventAr
  • Android 在通话期间播放音频文件[重复]

    这个问题在这里已经有答案了 对于我的 Android 应用程序 我想在从应用程序接听电话后播放音频文件 应用程序将发起电话呼叫 一旦接收者接听电话 应用程序应开始播放录制的音频文件 通过在谷歌上进行大量搜索 我发现这对于未root的设备来说
  • 无法读取解析推送通知包数据

    我尝试使用 Parse 推送通知服务发送自定义数据 但从 Bundle 中提取时总是返回 null 值 自定义广播接收器 Override public void onReceive Context context Intent inten
  • 使用 Android 的 Mobile Vision API 扫描二维码

    我跟着这个tutorial http code tutsplus com tutorials reading qr codes using the mobile vision api cms 24680关于如何构建可以扫描二维码的 Andr

随机推荐