2012-01-25 40 views
2

我试图在下午7点开放一个新的服务,它会在启动时通知我,但我无法解决这个问题,不能理解为什么。谁能帮我出thankx下面是代码我应该如何每天运行一个服务和下午12点使用alarmmanager


这是我在DaysCounterActivity类

Calendar calendar = Calendar.getInstance(); 
    calendar.set(Calendar.HOUR_OF_DAY, 19); 
    calendar.set(Calendar.MINUTE, 05); 
    calendar.set(Calendar.SECOND, 00); 


    Intent intent = new Intent(this, MyReciever.class); 
    PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0); 


    AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000 , pintent); 

写的代码,这里是MyReviever类onRevcieve方法

public void onReceive(Context context, Intent intent) { 
    // TODO Auto-generated method stub 

    Intent serviceIntent = new Intent(); 
    serviceIntent.setAction("com.saaram.MyService"); 
    context.startService(serviceIntent); 
} 
+0

还帮我关于相关的Android清单reciever的代码 – Saaram

回答

3

你的问题是pintent正在尝试运行服务,但MyReceiver是一个广播接收器。如果您将MyReceiver更改为服务,它会起作用。

public class MyReceiver extends Service { 


    public int onStartCommand(Intent intent, int flags, int startId) { 
     //Anything you put here is run at the time you set the alarm to 
     } 

    @Override 
    public IBinder onBind(Intent arg0) { 
     // TODO Auto-generated method stub 
     return null; 
     } 

    } 

在你的清单,你会只是这样声明:

<service android:name=".MyReceiver"></service> 
相关问题