Je suis en train d'écrire un service Android qui tire données du réseau de Instagram toutes les dix secondes, puis envoyer une notification s'il y a quelque chose de nouveau.service Android ne fonctionne que lorsque USB branché, l'erreur se produit après USB déconnecté
Il fonctionne très bien quand débogage USB est activé, (peut voir quelque chose sur Logcat) mais une erreur se produit après que je débranchez le câble USB!
Je ne sais pas déboguer ou vérifier le journal ce qui se passe là-bas, parce que l'erreur ne se produit que lorsque Logcat ne fonctionne pas ...
Voici mes codes de service (désolé pour la complexité. .)
@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
}
};
Puis sont tous sur les logiques de création de notifications.
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;
}
S'il vous plaît aidez-moi à trouver l'erreur potentielle ou me fournir des moyens de débogage, merci.