android - ACTION_BOOT_COMPLETED gives Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() -


i create static handler change ui texts , plots in activity. there timer created in bluetooth service. every second check database last message , update time. example, message comes in 1 second ago 2 seconds ago 3 seconds ago... store time when last message came in yyyy/mm/dd hh:mm:ss java date long value.

my problem @ line "if (homeactivity.mhandler != null) {" in service class below, gives error "caused by: java.lang.runtimeexception: can't create handler inside thread has not called looper.prepare()". happens when shut down , restart phone. action_boot_completed catch , trying run code, throws exception. know how fix it?

here codes:

activity:

public class homeactivity extends activity {     public static handler mhandler = new handler() {       public void handlemessage(final message msg) {          super.handlemessage(msg);           final bundle bundle = msg.getdata();           ...          }       };  } 

service:

public class bluetoothleservice extends service {     @override    public void oncreate() {        // doing other bluetooth stuffs here       ...        int one_second = 1*1000;       timer updatetimer = new timer();       updatetimer.scheduleatfixedrate(new timertask() {          @override          public void run() {             try {             if (homeactivity.mhandler != null) { // error                final message msg = homeactivity.mhandler.obtainmessage();                bundle bundle = new bundle();                 bundle.putstring("input", "update");                msg.setdata(bundle);                 homeactivity.mhandler.sendmessage(msg);             }          } catch (exception e) {               e.printstacktrace();          }       }      }, 0, one_second); } 

broadcast:

public class bluetoothbroadcast extends broadcastreceiver {     @override    public void onreceive(context context, intent intent) {        final string action = intent.getaction();        if (action.equals(intent.action_boot_completed)) {          intent = new intent(context, bluetoothleservice.class);          context.startservice(i);       }    } } 

androidmanifest:

<!-- start service when phone boot. --> <receiver android:name="com.example.bluetoothbroadcast" >    <intent-filter>    <action android:name="android.intent.action.boot_completed" />    </intent-filter> </receiver> 

probably should closer this:

private void createhandler() {      thread thread = new thread() {       public void run() {             looper.prepare();             final handler handler = new handler();             handler.postdelayed(new runnable() {                 @override                  public void run() {                     // work                      handler.removecallbacks(this);                     looper.mylooper().quit();                }              }, 2000);               looper.loop();         }      };      thread.start(); }  

Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -