开发者

Trying to recieve a string through a broadcast in an activity from a service, not seeing anything

Here's my code below. The first bit of code is for my activity. I put a click listener on a button in that activity that starts my service. Right now, I just want to see that I can recieve strings from that service so here is my code trying to do that. I put a "hello world" broadcast in the oncreate of the service to test. Can anyone spot the issue? The service code is below the activity code.

package homeBrewChatter.Calcs;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.Button;

import java.util.List;

public class Hop_Timer extends Activity {
    private TextView timerOut;

     private BroadcastReceiver onBroadcast = new BroadcastReceiver() {
            @Override
            public void onReceive(Context ctxt, Intent i) {
                Hop_Timer.this.timerOut = (TextView)Hop_Timer.this.findViewById(R.id.display_time);
                Hop_Timer.this.timerOut.setText("RECIEVED");
            }
    };

    @Override
    public void onCreate开发者_如何学运维(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hoptimer);

        Button setVars = (Button) findViewById(R.id.add_alarm_button);
        setVars.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                //Hop_Timer.this.timerOut = (TextView)Hop_Timer.this.findViewById(R.id.display_time);
                //Hop_Timer.this.timerOut.setText("working so far");
                startService(new Intent(Hop_Timer.this, Hop_Timer_Service.class));
            }

        });
    }

    public void onResume() {
        super.onResume();
        registerReceiver(onBroadcast, new IntentFilter("mymessage"));
    }
    public void onPause() {
        super.onPause();
        unregisterReceiver(onBroadcast);
    }  
}

package homeBrewChatter.Calcs;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.SystemClock;

public class Hop_Timer_Service extends Service {
    private Handler mHandler = new Handler();
    private int length_minutes;
    private boolean timer_running;
    private long startTime = 0L;
    public String currentTime = "";

    @Override
    public void onCreate() {
        length_minutes = 0;
        timer_running = false;
        getApplicationContext().sendBroadcast(new Intent("Hello World"));
    }

    public void setTime(int mins) {
        length_minutes = mins;
    }

    public void startTimer() {
        startTime = System.currentTimeMillis();
        mHandler.removeCallbacks(mUpdateTimerTask);
        mHandler.postDelayed(mUpdateTimerTask, 100);
    }

    private Runnable mUpdateTimerTask = new Runnable() {
        public void run() {
            final long start = startTime;
            long millis = SystemClock.uptimeMillis() - start;
            int seconds = (int) (millis/1000);
            int minutes = seconds /60;
            seconds = seconds %60;
            if(seconds < 10) {
                currentTime = "" + minutes + ":0" + seconds;
            } else {
                currentTime = "" + minutes + ":" + seconds; 
            }
            getApplicationContext().sendBroadcast(new Intent(currentTime));
            mHandler.postAtTime(this, start + (((minutes * 60) + seconds + 1) * 1000));
        }
    };

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
}


With respect to your actual question, the Intent that you are broadcasting does not match the IntentFilter you are using in registerReceiver(). Since your IntentFilter says it is looking for "mymessage" Intents, your Intent that you broadcast needs to have the "mymessage" action. Right now, one of your Intents has an action that is based on the current time, which is unlikely to be received by anything. The other has an action of "Hello World".

Beyond that:

  • By default, broadcasts are broadcast to the entire device. If you are intending to broadcast to the entire device, please use an action string that is namespaced to your app (e.g., homeBrewChatter.Calcs.mymessage). If you are not intending to broadcast to the entire device, please use setPackage() on the Intent that you broadcast to keep it within your application.

  • Since Service is a Context, you do not need to use getApplicationContext() in your Service for your broadcasts.

  • You do not appear to be calling startTimer().

  • It is infrequently a good idea, in production code, to have a Service whose primary job is to sit and watch time tick by. AlarmManager is a more common solution here, so your Service can be in memory only when it is adding actual value to the user.

  • You start the service, but never stop it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜