我的手机正在生成一个不停的Log.d输出。关于每秒1200次,以下一遍又一遍地重复。
04-25 15:58:04.883 1542-5012/? D/NetworkStatsCollection: getHistory:mUID 10266 isVideoCallUID: false
PID 1542是System_server,我已经了解它管理一系列Android服务。在我正在开发的应用程序中,我使用Alarm Manager和Notification服务,如下所示。有什么我可以做的让这项服务以它的方式作出反应吗?
public void scheduleNotification(Context context, long alarmTime, int notificationId, String setOrCancel) {
EditText questionView = (EditText)findViewById(R.id.question);
String questionText = questionView.getText().toString();
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentTitle(context.getString(R.string.app_name))
.setContentText(questionText)
.setAutoCancel(true)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(((BitmapDrawable) context.getResources().getDrawable(R.mipmap.ic_launcher)).getBitmap())
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
Intent intent = new Intent(context, DashboardActivity.class);
PendingIntent activity = PendingIntent.getActivity(context, notificationId, intent, PendingIntent.FLAG_CANCEL_CURRENT);
builder.setContentIntent(activity);
Notification notification = builder.build();
Intent notificationIntent = new Intent(context, TrackerNotificationService.class);
notificationIntent.putExtra(TrackerNotificationService.NOTIFICATION_ID, notificationId);
notificationIntent.putExtra(TrackerNotificationService.NOTIFICATION, notification);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, notificationId,
notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if (setOrCancel.equals("set")) {
alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTime, pendingIntent);
Log.d("Check it", "scheduleNotification: " + alarmTime);
}else{
alarmManager.cancel(pendingIntent);
}
}
TrackerNotificationService.java
import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class TrackerNotificationService extends BroadcastReceiver {
public static String NOTIFICATION_ID = "notification_id";
public static String NOTIFICATION = "notification";
@Override
public void onReceive(final Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = intent.getParcelableExtra(NOTIFICATION);
int notificationId = intent.getIntExtra(NOTIFICATION_ID, 0);
notificationManager.notify(notificationId, notification);
}
}