android - Opening the application from the background service -
android - Opening the application from the background service -
i have designed app receive text message(sms) , hence have implemented broadcast receiver on background service. works fine except cant open application(any activity class) background service. have written code start activity doesn't work.
i went through many similar questions, couldn't find solution problem.
this code snippet of service class:
public class background extends service { @override public int onstartcommand(intent intent, int flags, int startid) { // todo auto-generated method stub new broadcastreceiver() { string number, body; @override public void onreceive(context context, intent intent) { // todo auto-generated method stub bundle bundle = intent.getextras(); object[] messages = (object[]) bundle.get("pdus"); smsmessage[] sms = new smsmessage[messages.length]; (int n = 0; n < messages.length; n++) { sms[n] = smsmessage.createfrompdu((byte[]) messages[n]); } (smsmessage msg : sms) { body = new stringbuilder().append(msg.getmessagebody()) .tostring(); number = new stringbuilder().append( msg.getdisplayoriginatingaddress()).tostring(); } calendar c = calendar.getinstance(); // "5/11/2014 03:02:10" string date = c.get(calendar.day_of_month) + "/" + c.get(calendar.month) + "/" + c.get(calendar.year) + " " + c.get(calendar.hour) + ":" + c.get(calendar.minute) + "" + c.get(calendar.second); messagedata db = new messagedata(getapplicationcontext()); seek { db.open(); db.createentry(number, body, date, 0); db.close(); } grab (exception e) { // todo auto-generated grab block e.printstacktrace(); } intent intent2 = new intent(background.this, callingtimer.class); intent2.setflags(intent.flag_from_background); intent2.setflags(intent.flag_activity_brought_to_front); intent2.setflags(intent.flag_activity_clear_top); intent2.setflags(intent.flag_activity_new_task); startactivity(intent); } }; homecoming service.start_sticky; } @override public ibinder onbind(intent intent) { // todo auto-generated method stub homecoming null; } }
everything works fine
no, not.
you have created instance of anonymous inner class inherits broadcastreceiver
... , have done nil it. instance local variable of onstartcommand()
method, , instance go out of scope , garbage-collected shortly after onstartcommand()
returns.
to prepare this:
move broadcastreceiver
regular standalone public
java class.
add <receiver>
element in manifest pointing it, appropriate <intent-filter>
receive whatever broadcasts think want.
add <activity>
, if not have one, receiver start working after user launches activity.
delete service.
do not write service ever again, until larn why implementing service.
android android-activity service
Comments
Post a Comment