android - 如何在Android中实现通知服务(不使用Activity)

标签 android notificationmanager

我正在实现通知我的应用只是一个例子。

服务类文件:

public class Services 
{
public void myNotify(Context context,String message)
   {
      Log.v("my notification method","from GetNotification class");
      NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
      Notification notification = new Notification(0,"A new notification", System.currentTimeMillis());
      // Hide the notification after its selected
      notification.flags |= Notification.FLAG_AUTO_CANCEL;
      Log.v("services","11");
      //Intent intent = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse(""));
      Log.v("services","22");
      PendingIntent activity = PendingIntent.getActivity(context,0,new Intent(android.content.Intent.ACTION_VIEW,Uri.parse("")), 0);
      Log.v("services","33");
      notification.setLatestEventInfo(context,"Notification for you",message,activity);
      Log.v("services","44");
      notification.number += 1;
      Log.v("services","55");
      notificationManager.notify(0, notification);
      Log.v("services","66");
   }
}

AppActivity 主类:

public class AndroidPlatformservices extends Activity {
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
 Services str= new Services();
 str.myNotify(this,"wrwtrerwetwttwretwrterwterw"); 
}
}

在 log cat total file 方法中执行:

06-16 22:09:11.472: VERBOSE/my notification method(5128): from GetNotification class
06-16 22:09:11.472: VERBOSE/services(5128): 11
06-16 22:09:11.472: VERBOSE/services(5128): 22
06-16 22:09:11.483: VERBOSE/services(5128): 33
06-16 22:09:11.493: VERBOSE/services(5128): 44
06-16 22:09:11.503: VERBOSE/services(5128): 55
06-16 22:09:11.513: VERBOSE/services(5128): 66

问题是什么?

最佳答案

您必须在 Notification 构造函数中为图标使用正确的资源 ID,0 将不起作用。

关于android - 如何在Android中实现通知服务(不使用Activity),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6375569/

相关文章:

android - 为什么Android手机静止时传感器采样率变得太低?

java - 适用于 Android 的 Fitbit,每次启动应用程序时自动获取用户数据,无需 OAuth

java - Fragment 中的通知管理器

android - 收到推送通知时添加新通知(不替换以前的)

Android 应用因找不到资源而崩溃

android - 缩放位图分配失败 - Xamarin Visual Studio

安卓通知问题

Android 通知管理器在屏幕关闭时不起作用

android - Android 中的提醒不会重复

android - columnCount 必须大于或等于每个子项的 LayoutParams 中定义的所有网格索引(和跨度)的最大值