Welcome to OGeek Q&A 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...