通知仅在模拟器中显示 (API15)
Notification only displayed in Emulators (API15)
我是 运行 一项服务,它将检查 csv 文件中的条目并相应地显示通知。代码很好,该应用程序也适用于模拟器 运行 最小目标 API (API 15)。但是在我的所有手机中,运行 Jellybean 及以上,没有显示通知。
这是我的服务代码:
public class NotificationActivity extends IntentService {
public NotificationActivity() {
super("NotificationActivity");
}
@SuppressWarnings("deprecation")
@Override
protected void onHandleIntent(Intent intent) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
try {
URL csvURL = new URL("http://example.com/example.csv");
BufferedReader in = new BufferedReader(new InputStreamReader(csvURL.openStream()));
CSVReader reader = new CSVReader(in, ',', '\'', 1);
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(R.drawable.ic_launcher);
mBuilder.setContentTitle("Title");
mBuilder.setContentText(nextLine[4] + nextLine[13]);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT < 16) {
mNotificationManager.notify(0, mBuilder.getNotification());
} else {
mNotificationManager.notify(0, mBuilder.build());
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
stopSelf();
}
@Override
public void onDestroy() {
// I want to restart this service again in 30 mins
AlarmManager alarm = (AlarmManager)getSystemService(ALARM_SERVICE);
alarm.set(
alarm.RTC_WAKEUP,
System.currentTimeMillis() + (1000 * 60 * 30),
PendingIntent.getService(this, 0, new Intent(this, NotificationActivity.class), 0)
);
}
}
我意识到我不应该使用 StrictMode.setThreadPolicy,但它是一个周期性事件,所以我觉得它不会给应用程序带来太多麻烦。
为什么 API16 岁及以上不显示通知?
已修复。源码没有问题,但是我用的CSVReader(opencsv)可能不支持较新的API。由于它不支持,它从不执行 CSV Reader。我切换到 Apache Commons CSV,它工作正常。
我是 运行 一项服务,它将检查 csv 文件中的条目并相应地显示通知。代码很好,该应用程序也适用于模拟器 运行 最小目标 API (API 15)。但是在我的所有手机中,运行 Jellybean 及以上,没有显示通知。
这是我的服务代码:
public class NotificationActivity extends IntentService {
public NotificationActivity() {
super("NotificationActivity");
}
@SuppressWarnings("deprecation")
@Override
protected void onHandleIntent(Intent intent) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
try {
URL csvURL = new URL("http://example.com/example.csv");
BufferedReader in = new BufferedReader(new InputStreamReader(csvURL.openStream()));
CSVReader reader = new CSVReader(in, ',', '\'', 1);
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(R.drawable.ic_launcher);
mBuilder.setContentTitle("Title");
mBuilder.setContentText(nextLine[4] + nextLine[13]);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT < 16) {
mNotificationManager.notify(0, mBuilder.getNotification());
} else {
mNotificationManager.notify(0, mBuilder.build());
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
stopSelf();
}
@Override
public void onDestroy() {
// I want to restart this service again in 30 mins
AlarmManager alarm = (AlarmManager)getSystemService(ALARM_SERVICE);
alarm.set(
alarm.RTC_WAKEUP,
System.currentTimeMillis() + (1000 * 60 * 30),
PendingIntent.getService(this, 0, new Intent(this, NotificationActivity.class), 0)
);
}
}
我意识到我不应该使用 StrictMode.setThreadPolicy,但它是一个周期性事件,所以我觉得它不会给应用程序带来太多麻烦。
为什么 API16 岁及以上不显示通知?
已修复。源码没有问题,但是我用的CSVReader(opencsv)可能不支持较新的API。由于它不支持,它从不执行 CSV Reader。我切换到 Apache Commons CSV,它工作正常。