我想定期为echo 5秒发出一个intentservice。
在我的意图服务和广播接收器中我使用log.e
,但我没有登录logCat:
在我的FragmentActivity中:
public void scheduleAlarm(Context con) {
Intent intent = new Intent(con, CheckNotificationPerEach12HourBroadC.class);
final PendingIntent pIntent = PendingIntent.getBroadcast(con, 0,
intent, 0);
long firstMillis = System.currentTimeMillis(); // first run of alarm is immediate
AlarmManager alarm = (AlarmManager) con.getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, firstMillis, intervalMillis, pIntent);
}
我的BroadcastReceiver:
public class CheckNotificationPerEach12HourBroadC extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent i = new Intent(context, CheckNotificationPerEach12Hour.class);
Log.e("CheckNotificationPerEach12HourBroadC", "fired");
// i.putExtra("foo", "bar");
context.startService(i);
}
}
我的IntentService:
public class CheckNotificationPerEach12Hour extends IntentService{
public CheckNotificationPerEach12Hour( ) {
super("CheckNotificationPerEach12Hour");
// TODO Auto-generated constructor stub
}
@Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
Log.e("intent ser", "10 second");
}
}
<receiver
android:name="com.shadyab.utilities.CheckNotificationPerEach12HourBroadC"
android:process=":remote"
>
</receiver>
<service
android:name="com.shadyab.utilities.CheckNotificationPerEach12Hour"
android:exported="false"
/>
答案 0 :(得分:1)
创建服务,并在广播接收器中调用它。
public class ServiceTest extends Service {
private Timer mTimer;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
// getLogs();
mTimer = new Timer();
mTimer.schedule(timerTask, 5000, 5 * 1000);
}
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
Log.wtf("service","running")
}
};
}