android - Send notification when app is background -


i'm sending notification while app in background. notification sending when app background when click notification , open app app crashing. need send notification when app on background , have been doing ibeacon development.

protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);           notificationmanager = (notificationmanager) getsystemservice(context.notification_service);             ;              new foregroundchecktask().execute();      }       class foregroundchecktask extends asynctask<context, void, boolean> {            @override           protected boolean doinbackground(context... params) {                postnotificationubcity("entered region");             final context context = params[0].getapplicationcontext();              return isapponforeground(context);             }            private boolean isapponforeground(context context) {             activitymanager activitymanager = (activitymanager) context.getsystemservice(context.activity_service);             list<runningappprocessinfo> appprocesses = activitymanager.getrunningappprocesses();             if (appprocesses == null) {               return false;             }             final string packagename = context.getpackagename();             (runningappprocessinfo appprocess : appprocesses) {               if (appprocess.importance == runningappprocessinfo.importance_foreground && appprocess.processname.equals(packagename)) {                 return true;               }             }             return false;           }         } 

log :

11-19 18:31:01.300: e/androidruntime(13008): java.lang.runtimeexception: error occured while executing doinbackground() 11-19 18:31:01.300: e/androidruntime(13008):    @ android.os.asynctask$3.done(asynctask.java:299) 11-19 18:31:01.300: e/androidruntime(13008):    @ java.util.concurrent.futuretask.finishcompletion(futuretask.java:352) 11-19 18:31:01.300: e/androidruntime(13008):    @ java.util.concurrent.futuretask.setexception(futuretask.java:219) 11-19 18:31:01.300: e/androidruntime(13008):    @ java.util.concurrent.futuretask.run(futuretask.java:239) 11-19 18:31:01.300: e/androidruntime(13008):    @ android.os.asynctask$serialexecutor$1.run(asynctask.java:230) 11-19 18:31:01.300: e/androidruntime(13008):    @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1080) 11-19 18:31:01.300: e/androidruntime(13008):    @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:573) 11-19 18:31:01.300: e/androidruntime(13008):    @ java.lang.thread.run(thread.java:841) 11-19 18:31:01.300: e/androidruntime(13008): caused by: java.lang.arrayindexoutofboundsexception: length=0; index=0 11-19 18:31:01.300: e/androidruntime(13008):    @ com.example.beacon.mainactivity$foregroundchecktask.doinbackground(mainactivity.java:450) 11-19 18:31:01.300: e/androidruntime(13008):    @ com.example.beacon.mainactivity$foregroundchecktask.doinbackground(mainactivity.java:1) 11-19 18:31:01.300: e/androidruntime(13008):    @ android.os.asynctask$2.call(asynctask.java:287) 11-19 18:31:01.300: e/androidruntime(13008):    @ java.util.concurrent.futuretask.run(futuretask.java:234) 11-19 18:31:01.300: e/androidruntime(13008):    ... 4 more 

i wouldn't go asynctask purpose.

if want send notification when application in background mode, try using code on onpause() method:

/** * checks if application being sent in background (i.e behind * application's activity). *  * @param context context * @return <code>true</code> if application above one. */ public static boolean isapplicationsenttobackground(final context context) {  activitymanager = (activitymanager)    context.getsystemservice(context.activity_service);  list<runningtaskinfo> tasks = am.getrunningtasks(1);  if (!tasks.isempty()) {   componentname topactivity = tasks.get(0).topactivity;   if (!topactivity.getpackagename().equals(context.getpackagename())) {     return true;   }  }   return false; } 

add proper notification:

private void addnotification(context context, string message) {   int icon = r.drawable.ic_launcher;  long when = system.currenttimemillis();  string appname = context.getresources().getstring(r.string.app_name);  notificationmanager notificationmanager = (notificationmanager) context  .getsystemservice(context.notification_service);   notification notification;  pendingintent contentintent = pendingintent.getactivity(context, 0,  new intent(context, myactivity.class), 0);    notificationcompat.builder builder = new notificationcompat.builder(  context);  notification = builder.setcontentintent(contentintent)  .setsmallicon(icon).setticker(appname).setwhen(0)  .setautocancel(true).setcontenttitle(appname)  .setcontenttext(message).build();   notificationmanager.notify(0 , notification);   } 

don't forget include permission on androidmanifest:

<uses-permission android:name="android.permission.get_tasks" /> 

that should work.


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -