如何在android中实现Notification服务(不使用Activity)

前端之家收集整理的这篇文章主要介绍了如何在android中实现Notification服务(不使用Activity)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在实施通知我的应用程序只是示例

服务类文件

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,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总文件方法中执行:

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构造函数中为图标使用正确的ressource id,0将不起作用.
原文链接:https://www.f2er.com/android/430435.html

猜你在找的Android相关文章