android - 前景方法在 Android 的 Jellybean 中不起作用

标签 android chat background-foreground foreground-service

我已经为我的聊天应用程序实现了该服务,这个概念应该是在线的,并且最后一次看到就像 whatsapp 一样。

当我在我的应用程序中实现在线和最后一次看到的服务时,当我从 lollipop 到 lollipop 检查时,它工作 100% 正常,但是当我从 jellybean 到 lollipop 检查同样的东西时,它不再工作了。

@Override
    protected void onStop() {
        super.onStop();

        if (Utils.isAppSentToBackground(getApplicationContext())) {

            // call web Service here
            // Log.e("Angel", "in back");
            Utils.setInBackGround(getApplicationContext(), true);
            callOnlineOfflineService("0");
        }
    }


// code for resume called
    @Override
    protected void onResume() {
        super.onResume();
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
        if (Utils.isInBackGround(getApplicationContext())) {
            // Log.e("Angel", "From back");

            Utils.setInBackGround(getApplicationContext(), false);
            callOnlineOfflineService("1");
        }
    }

在这里,Util 是我的常用类,我在其中实现了常用方法,setInBackGround 方法将凭据设置到 SessionManager。

void callOnlineOfflineService(String status) {

        Log.e("status:::::online", "status" +status);

        if(!myclass.isConnected(getApplicationContext())){
            errordialog(getResources().getString(R.string.no_internet));
            return;
        }

        final List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("mode", "setUserOnlineStatus"));
        params.add(new BasicNameValuePair("userId", uid));
        params.add(new BasicNameValuePair("status", status));

        Log.e("status:::::online", "status" +params);

            Runnable runnable = new Runnable() {
                public void run() { 

                    UserFunctions userFunction = new UserFunctions(getApplicationContext());
                    JSONObject json = userFunction.CommonObject(params);

                        try {

                            final String s = json.getString("status");
                            Log.e("status:::::online", "status" +s);

                        } catch (JSONException e) {
                            e.printStackTrace();
                             return;
                        }
                }
            };
            Thread mythread = new Thread(runnable);
            mythread.start();
    }

我们将高度赞赏并提前感谢您的辛勤工作和努力。

最佳答案

我已经尝试并得出了这个结论并且它有效。

请确保您已授予以下权限,之后上述代码对任何人都可以正常工作,否则它不适用于某些操作系统。

<uses-permission android:name="android.permission.GET_TASKS" />

关于android - 前景方法在 Android 的 Jellybean 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31488280/

相关文章:

android - Android-每当显示 Activity 时,如何自动显示输​​入法选择器?

objective-c - iOS - 从 willEnterForeground 的 UIViewController 执行选择器

android - ImageView 未显示在 TableRow 上

java - Android 低功耗蓝牙

java - 使用java进行语音聊天,双向通信,低延迟

javascript - Comet VS Ajax 轮询

iOS - 从后台更新 UI

android - Cast 配套库的 RemoteMediaPlayer 媒体状态为 null

android - 在 gradle 中包含 Google Map API key 有哪些安全风险?

php - 简单的Python房间聊天服务器