2017-05-28 5 views
0

getrennt Ich schreibe einen Android Service die Netzwerkdaten von Instagram alle zehn Sekunden, und dann senden Benachrichtigung zieht, wenn es etwas Neues.Android Dienst funktioniert nur, wenn USB angeschlossen, Fehler auftreten, nachdem USB

Es funktioniert sehr gut, wenn USB-Debugging eingeschaltet ist, (etwas auf Logcat sehen) aber ein Fehler auftreten, nachdem ich den USB trennen!

Ich habe keine Ahnung, das Protokoll zu debuggen oder zu überprüfen, was dort vor sich geht, weil Fehler tritt nur auf, wenn Logcat nicht funktioniert ...

Hier sind über meine Service-Codes (sorry für die Komplexität. .)

@Override 
    public void onCreate() { 
     super.onCreate(); 
     // some initializations, fetch the access token, setup the API library .. 
     oauthSession = new InstagramOAuthSession(getApplicationContext()); 
     String accessToken = oauthSession.getAccessToken(); 
     instagramFacade = new InstagramFacadeImp(accessToken); 
     session = new InstagramDataSession(this); 
     try { 
      followers = session.readFollowers(); 
      Log.d("myLog", "Read Result: " + followers); 
     } catch (IOException e) { 
      Log.d("myLog","Io Error: " + e.getMessage()); 
     } 
    } 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    super.onStartCommand(intent,flags,startId); 
    Log.d("myLog", "Service start"); 
    handler.post(checkUnfollowedTask); //task is a runnable 
    return START_REDELIVER_INTENT; 
} 


private Runnable checkUnfollowedTask = new Runnable() { 
    @Override 
    public void run() { 
     AsyncTaskHelper.runAsyncTask(new AsyncTask<Void, Void, List<User>>() { 
      @Override 
      protected List<User> doInBackground(Void... voids) { 
       try { 
        //to pull some network data from instagram 
        FollowInfoViewModel model = instagramFacade.getFollowInfoViewModel(); 
        //save data into the internal storage 
        session.saveFollowers(model.getFollowerUsers()); 
        return model.getFollowerUsers(); 
       } catch (Exception e) { 
        Log.d("myLog","Error: " + e.getMessage()); 
       } 
       return null; 
      } 

      @Override 
      protected void onPostExecute(List<User> users) { 
       super.onPostExecute(users); 
       //some business logics 
       Log.d("myLog", "New follower: " + users); 
       followers.removeAll(users); 
       Log.d("myLog", "New unfollower: " + followers); 
       createNotification(followers); 
       followers = users; 
      } 
     }); 

     handler.postDelayed(this, 10000); // invoke every ten seconds 
    } 
}; 

Dann werden alle über die Logiken der Schaffung Benachrichtigungen sind.

private void createNotification(List<User> unfollowers) { 
    if (unfollowers.size() > 0) 
     Log.d("myLog", "Unfollower detected, creatre a notification."); 
    NotificationManager notificationManager = (NotificationManager) 
      getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); 

    List<Notification> notifications = buildNotifications(unfollowers); 
    for (int i = 0; i < notifications.size(); i++) 
     notificationManager.notify(i, notifications.get(i)); 
} 

@TargetApi(Build.VERSION_CODES.LOLLIPOP) 
private List<Notification> buildNotifications(List<User> unfollowers) { 
    List<Notification> notifications = new ArrayList<>(); 
    Context context = getApplicationContext(); 
    Notification.Builder builder = new Notification.Builder(context); 
    for (int i = 0; i < unfollowers.size(); i++) 
    { 
     User unfollower = unfollowers.get(i); 
     Intent intent = new Intent(this, MainActivity.class); 
     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP 
       | Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     PendingIntent pendingIntent = PendingIntent.getActivity(context, i, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
     Notification notification = builder.setSmallIcon(R.drawable.doge) 
       .setContentTitle(getString(R.string.receive_unfollow_notification)) 
       .setContentText(unfollower.getFull_name() + "(" + unfollower.getUsername() + ")"+ getString(R.string.unfollowed_you)) 
       .setDefaults(Notification.DEFAULT_ALL) 
       .setContentIntent(pendingIntent) 
       .setAutoCancel(true) 
       .setVisibility(Notification.VISIBILITY_PUBLIC) 
       .setPriority(Notification.PRIORITY_HIGH) 
       .build(); 
     notifications.add(notification); 
    } 

    return notifications; 
} 

mir bitte helfen, das Potenzial Fehler, um herauszufinden, oder mir ein paar Möglichkeiten zu debuggen bieten, danke.

Antwort