Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to run a application always in background to show a notification in android
    primarykey
    data
    text
    <p>I am trying to show a notification whenever any new data come into server. But I am facing a problem. It is only showing notification when i am opening the activity. But it is not running in background. I am using service to run the functionality in background. Can any one tell me how to run this application always in background, if the app closed then also?</p> <p>I am calling the service in onCreate method of the main activity like this:</p> <pre><code> public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.dashboard); startService(new Intent(this, ReviewUpdateService.class)); ..... } </code></pre> <p>and in ReviewUpdateService.class i have written this code: </p> <pre><code> public class ReviewUpdateService extends IntentService{ private Notification.Builder earthquakeNotificationBuilder; public static final int NOTIFICATION_ID = 1; public static String QUAKES_REFRESHED = "com.example.androidhive.QUAKES_REFRESHED"; private String user_id; JSONObject jsonData; private static String share_pref_file = "json_file"; JSONArray service_notification; int no_of_notify; public ReviewUpdateService() { super("ReviewUpdateService"); } private AlarmManager alarmManager; private PendingIntent alarmIntent; public void onCreate() { super.onCreate(); alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE); SharedPreferences prefs = getSharedPreferences(share_pref_file, Context.MODE_PRIVATE); String strJson = prefs.getString("jsondata", ""); if (!strJson.equals("")) { try { jsonData = new JSONObject(strJson); user_id = jsonData.getString(LoginActivity.KEY_UID); } catch (JSONException e) { e.printStackTrace(); } } String ALARM_ACTION = ReviewAlarmReceiver.ACTION_REFRESH_DASHBOARD_ALARM; Intent intentToFire = new Intent(ALARM_ACTION); alarmIntent = PendingIntent.getBroadcast(this, 0, intentToFire, 0); earthquakeNotificationBuilder = new Notification.Builder(this); earthquakeNotificationBuilder .setAutoCancel(true) .setTicker("New Review detected") .setSmallIcon(R.drawable.icon_1); } public void refreshEarthquakes(String formattedDate) { // Get the XML //URL url; UserFunctions userFunctions = new UserFunctions();; try { Log.d("user_id", user_id); JSONObject json_city = userFunctions .getNotification(user_id, formattedDate); if(json_city!=null){ { try { String notify_get_id = json_city.getString(LoginActivity.KEY_NO_NOTIFY); no_of_notify = Integer.parseInt(notify_get_id); service_notification = json_city .getJSONArray(LoginActivity.KEY_SERVICE_NOTIFY); } catch (JSONException e) { e.printStackTrace(); } } } if(service_notification!=null) { // looping through all item nodes &lt;item&gt; for (int i = 0; i &lt; service_notification.length(); i++) { try { JSONObject c = service_notification.getJSONObject(i); String dealer = c.getString("Dealer Rater"); String google = c.getString("Google+ Local"); String car = c.getString("Cars.com"); String edmunds = c.getString("Edmunds.com"); String yelp = c.getString("Yelp"); String yahoo = c.getString("Yahoo! Local"); String insider = c.getString("Insider Pages"); String city = c.getString("City Search"); int dealer2 = Integer.parseInt(dealer); int google2 = Integer.parseInt(google); int car2 = Integer.parseInt(car); int edmunds2 = Integer.parseInt(edmunds); int yelp2 = Integer.parseInt(yelp); int yahoo2 = Integer.parseInt(yahoo); int insider2 = Integer.parseInt(insider); int city2 = Integer.parseInt(city); String service_unit1 = "Dealer Rater"; String service_unit2 = "Google+ Local"; String service_unit3 = "Cars.com"; String service_unit4 = "Edmunds.com"; String service_unit5 = "Yelp"; String service_unit6 = "Yahoo! Local"; String service_unit7 = "Insider Pages"; String service_unit8 = "City Search"; if(dealer2 &gt; 0 ) { if(no_of_notify &gt; 0){ broadcastNotification(no_of_notify,service_unit1); } }else if(google2 &gt; 0 ) { if(no_of_notify &gt; 0){ broadcastNotification(no_of_notify,service_unit2); } }else if(car2 &gt; 0 ) { if(no_of_notify &gt; 0){ broadcastNotification(no_of_notify,service_unit3); } }else if(edmunds2 &gt; 0 ) { if(no_of_notify &gt; 0){ broadcastNotification(no_of_notify,service_unit4); } }else if(yelp2 &gt; 0 ) { if(no_of_notify &gt; 0){ broadcastNotification(no_of_notify,service_unit5); } }else if(yahoo2 &gt; 0 ) { if(no_of_notify &gt; 0){ broadcastNotification(no_of_notify,service_unit6); } }else if(insider2 &gt; 0 ) { if(no_of_notify &gt; 0){ broadcastNotification(no_of_notify,service_unit7); } }else if(city2 &gt; 0 ) { if(no_of_notify &gt; 0){ broadcastNotification(no_of_notify,service_unit8); } } } catch (JSONException e) { e.printStackTrace(); } } } // } } finally { } } @Override protected void onHandleIntent(Intent intent) { int updateFreq = 60; Calendar calender = Calendar.getInstance(); System.out.println("Current time =&gt; "+calender.getTime()); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm"); String formattedDate = df.format(calender.getTime()); Log.d("time", formattedDate); int alarmType = AlarmManager.ELAPSED_REALTIME_WAKEUP; long timeToRefresh = SystemClock.elapsedRealtime() + updateFreq*60*1000; alarmManager.setInexactRepeating(alarmType, timeToRefresh, updateFreq*60*1000, alarmIntent); refreshEarthquakes(formattedDate); sendBroadcast(new Intent(QUAKES_REFRESHED)); } private void broadcastNotification(int no_of_review, String service_name ) { Intent startActivityIntent = new Intent(this, DashboardActivity.class); PendingIntent launchIntent = PendingIntent.getActivity(this, 0, startActivityIntent, 0); earthquakeNotificationBuilder .setContentIntent(launchIntent) .setContentTitle( no_of_review+" "+"new review is there" ) .setContentText(service_name); if (no_of_review &gt; 10) { Uri ringURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); earthquakeNotificationBuilder.setSound(ringURI); } double vibrateLength = 100*Math.exp(0.53*no_of_review); long[] vibrate = new long[] {100, 100, (long)vibrateLength }; earthquakeNotificationBuilder.setVibrate(vibrate); int color; if (no_of_review &lt; 2) color = Color.GREEN; else if (no_of_review &lt; 5) color = Color.YELLOW; else color = Color.RED; earthquakeNotificationBuilder.setLights( color, (int)vibrateLength, (int)vibrateLength); NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(NOTIFICATION_ID, earthquakeNotificationBuilder.getNotification()); } } </code></pre> <p>And In a reciver method i have added a class like this:</p> <pre><code> public class ReviewAlarmReceiver extends BroadcastReceiver{ public static final String ACTION_REFRESH_DASHBOARD_ALARM = "com.example.androidhive.ACTION_REFRESH_EARTHQUAKE_ALARM"; @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub Intent startIntent = new Intent(context, ReviewUpdateService.class); context.startService(startIntent); } </code></pre> <p>}</p> <p>And in Android.manifest file i have modified like this:</p> <pre><code> ......... &lt;service android:enabled="true" android:name=".ReviewUpdateService"/&gt; &lt;receiver android:name=".ReviewAlarmReceiver"&gt; &lt;intent-filter&gt; &lt;action android:name="com.example.androidhive.ACTION_REFRESH_EARTHQUAKE_ALARM" /&gt; &lt;/intent-filter&gt; &lt;/receiver&gt; ........ </code></pre> <p>I don't know what is the problem running on. Can anyone help me? Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload