广播接收器发送通知继续

时间:2014-11-21 11:32:15

标签: android

我是android开发的新手。我正在开发一个应用程序,当我使用广播接收器在数据库中插入新行时,我会发送通知。我每隔10秒就调用一次这个函数。如果我在数据库中插入2行,那么我的应用程序会发送通知“收到2条新消息”。它的工作正常。问题是通知已在通知中显示,但我的应用程序每隔10秒连续发送通知。我想如果通知栏中显示相同行的通知,则不要重新发送通知。请帮我。这是我的代码..

package com.example.firstapp;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public class SampleBC extends BroadcastReceiver {

    static int noOfTimes = 0;
    private RegistrationOperation RegistrationOperation;

    // Method gets called when Broad Case is issued from MainActivity for every 10 seconds
    @Override
    public void onReceive(final Context context, Intent intent) {
        // TODO Auto-generated method stub
        noOfTimes++;
        //Toast.makeText(context, "BC Service Running for " + noOfTimes + " times", Toast.LENGTH_SHORT).show();
        AsyncHttpClient client = new AsyncHttpClient();
        RequestParams params = new RequestParams();


        RegistrationOperation = new RegistrationOperation(context);
        RegistrationOperation.open();

        String lastsyncdate = RegistrationOperation.LastSyncDate();
        params.put("date", lastsyncdate);

        final GlobalVariable glbvrbl = new GlobalVariable();

        // Checks if new records are inserted in Remote MySQL DB to proceed with Sync operation
        client.post(glbvrbl.path+"and_getrowcount.php",params ,new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(String response) {
                System.out.println(response);
                try 
                {

                    // Create JSON object out of the response sent by getdbrowcount.php
                    JSONObject obj = new JSONObject(response);
                    System.out.println(obj.get("count"));
                    // If the count value is not zero, call MyService to display notification
                    if(obj.getInt("count") != 0){

                        if(!(context.getPackageName().equalsIgnoreCase(
                                ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE))
                                        .getRunningTasks(1).get(0).topActivity.getPackageName())))
                        {
                            //app is in foreground;
                            final Intent intnt = new Intent(context, MyService.class);
                            // Set unsynced count in intent data
                            intnt.putExtra("intntdata", obj.getInt("count") + " New News Recieved");
                            // Call MyService
                            context.startService(intnt);
                        }


                    }else{
                        Toast.makeText(context, "Sync not needed", Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int statusCode, Throwable error,
                String content) {
                // TODO Auto-generated method stub
                if(statusCode == 404){
                    Toast.makeText(context, "404", Toast.LENGTH_SHORT).show();
                }else if(statusCode == 500){
                    Toast.makeText(context, "500", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(context, "Error occured!", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

}

1 个答案:

答案 0 :(得分:0)

我建议不要在您的方案中使用广播接收器,因为它可能会耗尽电池电量。 (因为哟每10秒检查一次)我建议您使用Android GCM,Google Cloud Server将代表您发送通知。请参考链接。 Link Link 2。希望这可能对你有所帮助。

干杯。