Intent指的是一个内部类

时间:2014-03-28 11:31:54

标签: java android android-intent intentservice

我想以Intent开头AlarmManager来引用内部类中的代码。

我在MainActivity.class中的代码

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
[...]
    DataProcessing2 dp = new DataProcessing2();

    startstop.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v1) {
            if(!listening){
                Log.i("Start","Button Pressed");
                listening = true;
                startstop.setText(R.string.stop);
                dp.start(getApplicationContext());
            }
        }
    });
}
}

我在DataProcessing2.class中的代码

public class DataProcessing2 {

public void start_accel(Context context){
    Log.i("Start Accel","Accessed");
    accel.setAccel(context);
    accel.listenAccel();

    Intent getAccelValuesIntent = new Intent(context, DataProcessing2.GetAccelValues.class);
    PendingIntent getValues = PendingIntent.getService(context, 0, getAccelValuesIntent, 0);
    getAccelData = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    getAccelData.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, System.currentTimeMillis(), sampling_acc_ms, getValues);
}

public class GetAccelValues extends IntentService {

    public GetAccelValues() {
        super("GetAccelValues");
        }

            [...]

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.i("Acc Values","Intent received");
        accel.calculateAccelerometerData();
        getCalculatedData();
        boolean moving = sD.acc_delta > sD.acc_th;

        Intent broadcastIntent = new Intent();
        broadcastIntent.setAction(NOTIFICATION_SERVICE);
        broadcastIntent.putExtra("name", "answerAccelerometer");
        broadcastIntent.putExtra("moving", moving);
        sendBroadcast(broadcastIntent);
    }

}

问题是我从未在GetAccelValues上获得Intent。

1 个答案:

答案 0 :(得分:0)

问题在于您使用的是不兼容的时钟。您应该AlarmManager.ELAPSED_REALTIME_WAKEUPSystem.elapsedRealtime()一起使用,或AlarmManager.RTC_WAKEUPSystem.currentTimeMillis()一起使用。例如:

getAccelData.setInexactRepeating(AlarmManager.RTC_WAKEUP,
        System.currentTimeMillis(), sampling_acc_ms, getValues);