0

我已经设置好了,当用户点击“打卡”时,它会启动 IntentService,然后 Intent 服务会更新 UI,唯一的问题是它不工作....这是我的主要活动页面代码:

    package com.famousmods.payme;

    import java.util.Timer;
    import java.util.TimerTask;

    import android.os.Bundle;
    import android.app.Activity;
    import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
import android.graphics.PorterDuff;

public class PayTracker extends Activity {
private ResponseReceiver receiver;
private static double Reserve;
private static int Reserve1;
public static double money;
public static double counter;
private static int go;
private static int countdown;
public static int convert;
public static double HW;
public static double OTW;
public static double HPD;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pay_tracker);
    getActionBar().setDisplayHomeAsUpEnabled(true);
 // register reciver
    IntentFilter filter = new IntentFilter(ResponseReceiver.ACTION_RESP);
    filter.addCategory(Intent.CATEGORY_DEFAULT);
    receiver = new ResponseReceiver();
    registerReceiver(receiver, filter);
 // Receive messages from options page
   double pHW, pOTW, pHPD;
    Intent intent = getIntent();
    pHW = intent.getDoubleExtra(Options.MESSAGE_HW, 0);
    pOTW = intent.getDoubleExtra(Options.MESSAGE_OTW, 0);
    pHPD = intent.getDoubleExtra(Options.MESSAGE_HPD, 0);
    if(pHW != 0){
        HW = pHW;
        OTW = pOTW;
        HPD = pHPD;
    }
 // Color buttons
    Button buttonc = (Button) findViewById(R.id.clockin);
    buttonc.getBackground().setColorFilter(0xFF00FF00, PorterDuff.Mode.MULTIPLY);
    Button buttond = (Button) findViewById(R.id.clockout);
    buttond.getBackground().setColorFilter(0xFFFF0000, PorterDuff.Mode.MULTIPLY);



     final Button b = (Button) findViewById(R.id.clockout);
     b.setOnClickListener(new OnClickListener() {
         public void onClick(View v) {


             }
     });

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_pay_tracker, menu);
    return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
    };
    return super.onOptionsItemSelected(item);
}

public void sendMessage(View view) {

    //Start IntentService
    Intent msgIntent = new Intent(this, SimpleIntentService.class);
    msgIntent.putExtra(SimpleIntentService.PARAM_IN_HW, HW);
    msgIntent.putExtra(SimpleIntentService.PARAM_IN_OTW, OTW);
    msgIntent.putExtra(SimpleIntentService.PARAM_IN_HPD, HPD);
    startService(msgIntent);//}   
}

public class ResponseReceiver extends BroadcastReceiver {
    public static final String ACTION_RESP = "com.famousmods.payme.action.MESSAGE_PROCESSED";
    @Override
    public void onReceive(Context context, Intent intent) {

        // Update UI, new "message" processed by SimpleIntentService
        final TextView t1 = (TextView) findViewById(R.id.yourpay);
        t1.setTextColor(Color.parseColor("#008000"));
        final TextView t2 = (TextView) findViewById(R.id.payper);
       String end = intent.getStringExtra(SimpleIntentService.PARAM_OUT_END);
       String result = intent.getStringExtra(SimpleIntentService.PARAM_OUT_RESULT);
       t2.setText(result);
       t1.setText(end);
    }

}

@Override
public void onDestroy() {
    this.unregisterReceiver(receiver);
    super.onDestroy();  


}

}

这是我的意图服务页面,它的设置是为了在意图启动时启动一个计时器,每 20 毫秒应该将新意图发送回活动页面,这是代码:

    package com.famousmods.payme;

import java.util.Timer;
import java.util.TimerTask;

import com.famousmods.payme.PayTracker.ResponseReceiver;

import android.app.IntentService;
import android.content.Intent;

public class SimpleIntentService extends IntentService {

public static final String PARAM_OUT_END = "end";
public static final String PARAM_OUT_RESULT = "result";
public static final String PARAM_IN_HW = "hourly wage";
public static final String PARAM_IN_OTW = "overtime";
public static final String PARAM_IN_HPD = "hours per day";
public static String end;
public static String result;
private static double Reserve;
private static int Reserve1;
public static double money;
public static double counter;
private static int go;
private static int countdown;
public static int convert;

public SimpleIntentService() {
    super("SimpleIntentService");

}

@Override
protected void onHandleIntent(Intent intent) {
    // recieve from paytracker
    double HW = intent.getDoubleExtra(PARAM_IN_HW, 1);
    double OTW = intent.getDoubleExtra(PARAM_IN_OTW, 1);
    double HPD = intent.getDoubleExtra(PARAM_IN_HPD, 1);
    // Calculate pay per second
    final double PPS = (HW/3600);
    final double DPPS = (PPS/50);
    final double OTPPS = (OTW/3600);
    final double DOTPPS = (OTPPS/50);
    final double HPDPS = (HPD*3600);
    final double DHPDPS = (HPDPS*50);

    Timer t = new Timer();
    t.schedule(new TimerTask() {
        @Override
        public void run() {
            new Runnable() {

                public void run() {
                    if(DHPDPS==0){
                        money = (DPPS+Reserve);
                        Reserve = (money);
                        end = String.format("%1f", money);
                       //t1.setText("$" + end);
                    }else if(counter > DHPDPS && DOTPPS != 0 && DHPDPS != 0){
                        money = (DOTPPS+Reserve);
                        Reserve = (money);
                        end = String.format("%1f", money);
                        //t1.setText("$" + end);
                    } else{

                        money = (DPPS+Reserve);
                        Reserve = (money);
                        end = String.format("%1f", money);
                        //t1.setText("$" + end);
                    }
                    counter++;
                    //if(counter == 3000)
                    //   t.cancel();

                    // Display pay per second
                    if(counter <= DHPDPS || DHPDPS == 0){
                        result = String.format("%.8f", PPS);
                    }else{
                    //  t2.setText("Your pay per second is: $"+result2);
                        result = String.format("%.8f", OTPPS);
                    }

                 // Broadcast intent
                    Intent broadcastIntent = new Intent();
                    broadcastIntent.setAction(ResponseReceiver.ACTION_RESP);
                    broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
                    broadcastIntent.putExtra(PARAM_OUT_END, end);
                    broadcastIntent.putExtra(PARAM_OUT_RESULT, result);
                    sendBroadcast(broadcastIntent);
                }
            }; 
        }
    }, 20, 20);
}

}

谢谢大家!

4

0 回答 0