---------UPDATE -----------------------------------------
When the app starts I receive numberformat exception at line :
final long thetime=Long.parseLong(time_value);
But the above aren't in the main activity...
In the xml file I have in the edittex
android:inputType="number" .
This line is in myservice class in which I have the alarmamanager(note I can't use catch because below(alarm.setRepeating) it doesn't recognize "thetime" value.
protected void onHandleIntent(Intent intent) {
//try{
String time_value;
time_value=(String) intent.getStringExtra("time_value");
final long thetime=Long.parseLong(time_value);// }
// catch (NumberFormatException e) {
//}
mContext = getApplicationContext();
mHandler.post(new Runnable(){
@Override
public void run() {
// Start service using AlarmManager
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 10);
Intent intent = new Intent(myservice.this,selection.class);
PendingIntent pintent = PendingIntent.getService(myservice.this, 0, intent,
0);
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
thetime*1000, pintent);
// Tell the user about what we did.
Toast.makeText(myservice.this, "Configured time interval",
Toast.LENGTH_LONG).show();
}
});
}
}
I load the time_value from another activity:
public void onClick(View v) {
switch (v.getId()){
case R.id.btn:
edit11=edit1.getText().toString();
edit22=edit2.getText().toString();
Intent i=new Intent(this,selection.class);
i.putExtra("code_value",edit11);
Intent k=new Intent(this,myservice.class);
k.putExtra("time_value",edit22);
this.startService(k);
Intent l=new Intent(this,MainActivity.class);
startActivity(l);
break;
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…