Android LocalBroadcast经理onReceive无法正常工作

时间:2016-03-01 07:22:17

标签: java android android-broadcastreceiver localbroadcastmanager

我尝试在我的android服务类中使用localbroadcastreceivers。但是当它运行时,主活动中的onReceive方法不会被调用。我已经看了几个例子和教程,但找不到问题。

主要活动

BroadcastReceiver receiver;


    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        LocalBroadcastManager.getInstance(this).registerReceiver((receiver), 
                new IntentFilter(TestService.COPA_MESSAGE)
            );
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
         LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver);
        super.onStop();
    }

onCreate Method

receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String s = intent.getStringExtra(TestService.COPA_MESSAGE);
                Toast.makeText(getApplicationContext(),"Result - " + s, Toast.LENGTH_LONG).show();
                System.out.println("Result - " + s);
                // do something here.
            }
        };

测试服务类

LocalBroadcastManager broadcaster;
    static final public String COPA_RESULT = "com.controlj.copame.backend.COPAService.REQUEST_PROCESSED";
    static final public String COPA_MESSAGE = "com.controlj.copame.backend.COPAService.COPA_MSG";

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");

    }


    public void sendResult(String message) {
        Intent intent = new Intent(COPA_RESULT);
        if(message != null)
            intent.putExtra(COPA_MESSAGE, message);
            broadcaster.sendBroadcast(intent);
    }


    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), "Service Created", 1).show();
        broadcaster = LocalBroadcastManager.getInstance(this);
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), "Service Destroy", 1).show();
        super.onDestroy();
    }



public void sendResult(String message) {
        Intent intent = new Intent(COPA_RESULT);
        if(message != null)
            intent.putExtra(COPA_MESSAGE, message);
            broadcaster.sendBroadcast(intent);
            LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    }


@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), "Service Running ", 1).show();


            class HttpAsync extends AsyncTask<String, Void, String> {


            @Override
            protected String doInBackground(String... params) {

                String result = null; 
                MyDefaultHttpClient client = MyDefaultHttpClient.getInstance();
                HttpPost httpPost = new HttpPost("url");
                try {

                    List<NameValuePair> data = new ArrayList<NameValuePair>();
                    data.add(new BasicNameValuePair("mob_no", "0112888788"));

                    httpPost.setEntity(new UrlEncodedFormEntity(data));
                    HttpResponse responce = client.execute(httpPost);
                    InputStream in = responce.getEntity().getContent();
                    BufferedReader br = new BufferedReader(
                    new InputStreamReader(in));
                    StringBuilder responceStr = new StringBuilder();
                    String responceLineStr = null;
                    while ((responceLineStr = br.readLine()) != null) {
                        responceStr.append(responceLineStr);
                    }
                    br.close();
                    in.close();
                    result = responceStr.toString();
                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                return result;
            }

            @Override
            protected void onPostExecute(String result) {
                // TODO Auto-generated method stub
                super.onPostExecute(result);
                if ((result != null) || (result != "")){
                    System.out.println("Myresult"+ result);

                    try {   
                        sendResult("My Test");
                        PushNotification(getApplicationContext());
                        String recData[] = result.split("#");
                        System.out.println("Moblile  "+ recData[0]);    
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        HttpAsync a = new HttpAsync();
        a.execute();

        return super.onStartCommand(intent, flags, startId);
    }

0 个答案:

没有答案