java - Android - Vibrate without displaying new Activity? -


i have following code in android app. is, @ specified time passed calendar when variable, opens runningactivity (which blank), vibrates, , sends notification. if i've pressed home button , it's running in background, starts new blank runningactivity , vibrates , sends notification. i'm trying figure out how stuff in runningactivity (vibrate , send notification) without opening blank runningactivity, allowing application stay in background.

i not need calling notification or vibration. need know how run actions in runningactivity oncreate @ specific time witout opening/showing runningactivity. seen below, setup pendingintent alarmmanagager, issue is launching activity , showing when want vibrate/send notification.

public void startalarm(activity activity, calendar when){            currenttimerhour = when.get(calendar.hour);     currenttimermin = when.get(calendar.minute);      intent intent = new intent(activity, runningactivity.class);     pendingintent pendingintent = pendingintent.getactivity(activity, 12345, intent, pendingintent.flag_cancel_current);     alarmmanager = (alarmmanager)activity.getsystemservice(activity.alarm_service);     am.set(alarmmanager.rtc_wakeup, when.gettimeinmillis(), pendingintent); } 

runningactivity class:

public class runningactivity extends activity { @override public void oncreate(bundle bundle){     super.oncreate(bundle);      vibrator v = (vibrator) getsystemservice(context.vibrator_service);     v.vibrate(500);      notificationcompat.builder mbuilder;     mbuilder = new notificationcompat.builder(this)             .setsmallicon(r.drawable.ic_launcher)             .setcontenttitle("my notification")             .setcontenttext("hello world!");      intent resultintent = new intent(this, mainactivity.class);      taskstackbuilder stackbuilder = taskstackbuilder.create(this);     stackbuilder.addparentstack(mainactivity.class);     stackbuilder.addnextintent(resultintent);     pendingintent resultpendingintent =             stackbuilder.getpendingintent(                     0,                     pendingintent.flag_update_current             );     mbuilder.setcontentintent(resultpendingintent);     notificationmanager mnotificationmanager =             (notificationmanager) getsystemservice(context.notification_service);     mnotificationmanager.notify(0, mbuilder.build()); } 

}

to send notification background:

public void createnotification(context context) {          // prepare intent triggered if         // notification selected         intent intent = new intent(context,myexample.class);          pendingintent pintent = pendingintent.getactivity(context, 0, intent, 0);            notification noti = new notification.builder(context)         .setcontenttitle("my title")         .setcontenttext("my message.")         .setsmallicon(r.drawable.app_icon)          .setcontentintent(pintent).build();          @suppresswarnings("static-access")         notificationmanager notificationmanager =          (notificationmanager)context.getsystemservice(context.notification_service);         // hide notification after selected         noti.flags |= notification.flag_auto_cancel;           noti.flags |= notification.flag_show_lights;      notificationmanager.notify(0, noti);  } 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -