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

5

我将实现我的应用程序中的通知功能,这只是一个示例。

服务类文件:

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"); 
}
}

在日志窗口中,总共执行了以下文件方法:

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

问题是什么?


当你在想问题出在哪里时,我在想你的问题是什么?这个程序没有达到你的预期吗?如果不是,那么错误在哪里?你是想创建一个Android服务还是使用一个名为Service的类? - Greg Giacovelli
嗨 Greg,程序存在问题,虽然可以成功执行,但无法显示状态栏通知,也就是问题所在。 - Narasimha
1个回答

4

在通知构造函数中,您必须使用适当的资源ID来表示图标,0不起作用。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接