android - Why didn`t it work the imported function in SMSReceived from MainActivity? -


can tell me please why didn`t work imported function in smsreceived mainactivity.

package com.example.smarthome;  import android.content.broadcastreceiver; import android.content.context; import android.content.intent; import android.os.bundle; import android.telephony.smsmessage; import android.widget.toast;  public class smsreceiver extends broadcastreceiver {     mainactivity mainactivityobject=new mainactivity();     @override     public void onreceive(context context, intent intent) {         bundle bundle = intent.getextras();         if (bundle != null){             toast.maketext(context, "newsms", toast.length_long).show();             mainactivityobject.afisarestareled();             } } } 

the function mainactivity is:

protected void afisarestareled() {     textview view = new textview(this);     uri urismsuri = uri.parse("content://sms/inbox");     cursor cursor = getcontentresolver().query(urismsuri, null, null, null, null);     while (cursor.movetonext()){          if(cursor.getstring(2).equals(number) && cursor.getstring(8).equals("0")){             if(cursor.getstring(13).endswith("aprins") || cursor.getstring(13).endswith("stins")){                 textview textstareled = (textview) findviewbyid(r.id.textstareled);                 textstareled.settext(cursor.getstring(13));                 break;             }         }       }       markmessageread();     } 

p.s. want make function wich should modify textview when receive sms.

you cannot create instance of activity yourself, doing in onreceive():

mainactivity mainactivityobject=new mainactivity(); 

only android framework can create instance of activity correctly, needs have context set , android framework this.

in activity can create instance of broadcastreceiver (you can use inner class of activity) , register using registerreceiver(). then, in onreceive() have access methods of activity , can call 1 update ui.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -