2016-04-12 8 views
2

Ich entwickle eine App, die eine Benachrichtigung sendet, wenn der Benutzer mehr als 300 Meter bewegt hat ODER wenn nicht bewegt, dann nach 2 Stunden.Wie wird die App im Hintergrund ausgeführt, um zeitnahe Benachrichtigungen zu senden?

Das Problem ist jedoch, dass, wenn ich die Zeit auf 2 Minuten ändere, scheint es zu funktionieren, aber wenn ich die Zeit auf 2 Stunden ändere, sendet es mir keine Benachrichtigung. Kann die App in den Schlafmodus gehen? Ich weiß es nicht und auch auf Distanzänderung sendet es keine Benachrichtigung.

Hier ist meine Mitteilung zu senden Code, TrackMe.Java

package miniandroid.com.services; 

import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.app.Service; 
import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.os.Handler; 
import android.os.IBinder; 

import miniandroid.com.activities.MainActivity; 


public class TrackMeService extends Service { 

    public int onStartCommand(Intent intent, int flags, int startId) { 
     Handler handler = new Handler(); 
     handler.postDelayed(new Runnable(){ 
      public void run(){ 

     SharedPreferences prefs = getSharedPreferences("emood_veriif", MODE_PRIVATE);  
      String mapfirst = prefs.getString("MapFirst", ""); 
      String mapsecond = prefs.getString("MapSecond", ""); 
      if (mapfirst != mapsecond){ 
       NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
       Notification mnotify = new Notification(); 
       Context context = getApplicationContext(); 
       Intent intent = new Intent(context,MainActivity.class); 
       PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0); 
       Notification.Builder builder = new Notification.Builder(context); 
       builder.setDefaults(Notification.DEFAULT_SOUND); 
       builder.setAutoCancel(false); 
       builder.setTicker("This is important. You have unanswered questions in Smood App."); 
       builder.setContentTitle("sMood"); 
       builder.setContentText("Please describe your mood at this location."); 
       builder.setSmallIcon(android.R.drawable.stat_notify_more); 
       builder.setContentIntent(pending); 
       builder.setOngoing(true); 
       builder.setNumber(100); 
       builder.build(); 
       mnotify= builder.getNotification(); 
       nm.notify(11,mnotify); 
      SharedPreferences prefsy = getSharedPreferences("emood_veriif", MODE_PRIVATE); 
      SharedPreferences.Editor editor = prefsy.edit(); 
      editor.putString("MapSecond", mapfirst); 
      editor.commit(); 
      } 
      else{ 

      } 

      } 
     }, 1); 


    return super.onStartCommand(intent, flags, startId); 

    } 


    public void onDestroy() { 

    super.onDestroy(); 
    } 


    public IBinder onBind(Intent arg0) { 

    return null; 
    } 


} 

Hier ist der Code für den Standort-Service, LocationService.Java

package miniandroid.com.services; 

import android.app.Service; 
import android.content.Context; 
import android.content.Intent; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.os.IBinder; 
import android.util.Log; 
import android.widget.Toast; 

import miniandroid.com.helpers.UserPreference; 

public class LocationService extends Service { 
    public static final String BROADCAST_ACTION = "Hello World"; 
    //private static final int TIME_DIFF = 1000 * 60 * 2; // 2 minutes 
    private static final int TIME_DIFF = 1000 * 60 * 60 * 2; //2 hours 
    private static final int DISTANCE_DIFF = 300; 
    public LocationManager locationManager; 
    public MyLocationListener listener; 
    public Location previousBestLocation = null; 

    Intent intent; 
    int counter = 0; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     intent = new Intent(BROADCAST_ACTION); 
    } 

    @Override 
    public void onStart(Intent intent, int startId) { 
     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     listener = new MyLocationListener(); 
     locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, listener); 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener); 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 

    protected boolean isBetterLocation(Location location, Location currentBestLocation) { 
     if (currentBestLocation == null) { 
      // A new location is always better than no location 
      return true; 
     } 

     // Check whether the new location fix is newer or older 
     long timeDelta = location.getTime() - currentBestLocation.getTime(); 
     float distanceDelta = location.distanceTo(currentBestLocation); 


     boolean isSignificantlyNewer = timeDelta > TIME_DIFF; 

     Log.d("MyLocationListener","onLocationChanged: Time Delta: "+ timeDelta+" Distance Delta: "+distanceDelta); 

     // If it's been more than two minutes since the current location, use the new location 
     // because the user has likely moved 
     if (isSignificantlyNewer) { 

      Log.d("MyLocationListener","onLocationChanged: Time Delta: is appropriate should show notification Time Delta: "+timeDelta); 
      return true; 
      // If the new location is more than two minutes older, it must be worse 
     } 


     if(distanceDelta > DISTANCE_DIFF) 
     { 
      Log.d("MyLocationListener","onLocationChanged: Distance Delta: is appropriate should show notification Distance Delta: "+distanceDelta); 
      return true; 
     } 
     return false; 
    } 

    /** 
    * Checks whether two providers are the same 
    */ 
    private boolean isSameProvider(String provider1, String provider2) { 
     if (provider1 == null) { 
      return provider2 == null; 
     } 
     return provider1.equals(provider2); 
    } 

    @Override 
    public void onDestroy() { 
     // handler.removeCallbacks(sendUpdatesToUI); 
     super.onDestroy(); 
     Log.v("STOP_SERVICE", "DONE"); 
     locationManager.removeUpdates(listener); 
    } 

    public static Thread performOnBackgroundThread(final Runnable runnable) { 
     final Thread t = new Thread() { 
      @Override 
      public void run() { 
       try { 
        runnable.run(); 
       } finally { 

       } 
      } 
     }; 
     t.start(); 
     return t; 
    } 


    public class MyLocationListener implements LocationListener { 

     public void onLocationChanged(final Location loc) { 
      if (isBetterLocation(loc, previousBestLocation)) { 
       previousBestLocation = loc; 
       startService(new Intent(LocationService.this,TrackMeService.class)); 
      } 
     } 

     public void onProviderDisabled(String provider) { 
      Toast.makeText(getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT).show(); 
     } 


     public void onProviderEnabled(String provider) { 
      Toast.makeText(getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show(); 
     } 


     public void onStatusChanged(String provider, int status, Bundle extras) { 

     } 

    } 
} 

Die andere Klasse, location.java, ist die Bestimmung der Lage mit lat-lng und geocoding es auch zu getAdress, aber ich benutze nur lat-lng. Ich stelle es auf die Position nach alle zwei Minuten und 10 Meter wechselt. ist das in Ordnung?

// How many Geocoder should return our GPSTracker 
    int geocoderMaxResults = 1; 

    // The minimum distance to change updates in meters 
    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters 

    // The minimum time between updates in milliseconds 
    private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 2; // 2 minutes 

Jede sorta Hilfe wird sehr geschätzt. Danke.

Antwort

0

Um die App zu halten nach wie vor Empfang einer Benachrichtigung, wenn es nicht ausgeführt wird, versuchen WAKE_LOCK Erlaubnis hinzuzufügen:

<uses-permission android:name="android.permission.WAKE_LOCK" /> 
+0

Es wird das Handy nur für diese App wach halten? –

+0

Ja also füge einfach hinzu, wenn du wirklich –

+0

hi bro brauchst, habe ich das hinzugefügt, aber es macht meinen Handy-Bildschirm nicht aus, ist es möglich, dass es nur CPU benutzt, bleibt der Bildschirm nicht wach und es trotzdem schickt mir eine Benachrichtigung? –

0

Mai werden Sie den Vordergrund Dienste werden sollen. http://developer.android.com/guide/components/services.html#Foreground

Vordergrunddienste werden vom Betriebssystem nicht beendet.

Bitte auch Standortupdate Mindestdauer erhöhen sonst wird die App die Batterie noch vor diesen 2 Stunden entladen;) Wie ich sehe senden Sie 0 als minTime und minDistance an requestLocationUpdates locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener); .

+0

oh, okay. lass mich im Vordergrund Dienste einarbeiten. Danke für den Tipp, was könnte die richtige Zeit und Entfernung sein, um die Updates zu erhalten? –

Verwandte Themen