这里是代码:
public class MyServeice extends Service
{
private Timer pushTimer;
private final int NOTEF_ID = 1234;
NotificationManager manager;
@Override
public IBinder onBind(Intent arg0)
{
return null;
}
@Override
public void onCreate()
{
Log.i("MyActivity", "1");
//pushTimer = new Timer();
manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
}
@Override
public void onDestroy()
{
Log.i("MyActivity", "2");
//pushTimer.cancel();
}
@Override
public void onStart(Intent intent, int startid)
{
Log.i("MyActivity", "3");
//pushTimer.schedule(new TimerTask()
//{
// @Override
// public void run()
// {
Notification not = new Notification(R.drawable.ic_launcher, "Custom notification", System.currentTimeMillis());
PendingIntent notIntent = PendingIntent.getActivity(this , 0, new Intent(this, MainActivity.class), 0);
not.setLatestEventInfo(this, "Title", "Text", notIntent);
manager.notify(NOTEF_ID, not);
manager.cancel(NOTEF_ID);
// }
//}, 0L, 60L * 1000);
}
}
我尝试从我的 MainActivity 活动类开始它(在我认为问题出在计时器之前,但现在我评论它)。此处开始代码:
startService(new Intent(this, MyServeice.class));
没有显示来自服务类的日志,所以我决定根本没有启动服务。应用程序不会崩溃并正常启动。你能检查我的代码吗?