Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

android - numberformat exception using alarmamanager - crashes before on start

---------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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
because I made lonfitude and latitude Strings (I had them as double before). How can I overcome that?

To get String from double use String.valueOf();

String latitude = String.valueOf(location.getLatitude());

Repeat

Also,how can I select to send the data in time intervals that the user will define?

To repeat at specific interval use AlarmManager.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...