AlarmManager与PendingIntent的联合使用(二)
2014-02-28 20:44
281 查看
PendingIntent.getService配合AlarmManager,自定义个类继承Service:
package com.example.alarmmanager;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service {
public IBinder onBind(Intent intent) {
return null;
}
public void onCreate()
{
Log.d("MyService", "onCreate");
super.onCreate();
}
public void onStart(Intent intent, int startId) {
Log.d("MyService", "onStart");
super.onStart(intent, startId);
}
public void onDestroy() {
Log.d("MyService", "onDestroy");
super.onDestroy();
}
}
将sevice类配置到Manifeset文件中:
<service
android:enabled = "true"
android:name = ".MyService">
</service>
添加按钮触发的事件:
Button btnService = (Button)findViewById(R.id.btnService);
btnService.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
AlarmManager am = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(MainActivity.this, MyService.class);
PendingIntent pi = PendingIntent.getService(MainActivity.this, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
long time = System.currentTimeMillis();
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, time, 10000, pi);
Log.d("btnService", "onClick");
}
});
package com.example.alarmmanager;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service {
public IBinder onBind(Intent intent) {
return null;
}
public void onCreate()
{
Log.d("MyService", "onCreate");
super.onCreate();
}
public void onStart(Intent intent, int startId) {
Log.d("MyService", "onStart");
super.onStart(intent, startId);
}
public void onDestroy() {
Log.d("MyService", "onDestroy");
super.onDestroy();
}
}
将sevice类配置到Manifeset文件中:
<service
android:enabled = "true"
android:name = ".MyService">
</service>
添加按钮触发的事件:
Button btnService = (Button)findViewById(R.id.btnService);
btnService.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
AlarmManager am = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(MainActivity.this, MyService.class);
PendingIntent pi = PendingIntent.getService(MainActivity.this, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
long time = System.currentTimeMillis();
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, time, 10000, pi);
Log.d("btnService", "onClick");
}
});
相关文章推荐
- AlarmManager与PendingIntent的联合使用(一)
- alarmManager和prendingIntent的联合使用
- Android 的 AlarmManager 和 wakeLock联合使用
- AlarmManager的使用
- mysql选择联合索引还是单索引?索引列应该使用哪一个最有效?深入测试探讨
- Android中的AlarmManager的使用
- OpenCV2.3.1和VS2010联合使用的设置
- RXJava与Retrofit联合使用
- AlarmManager使用总结
- AlarmManager使用
- struts2 的 @Result 和 @ResultPath 联合使用
- UIPageController与UIScrollView的联合使用
- Git@OSC 开源中国git与sourceTree联合使用教程
- linux find/grep 与cat联合使用
- Perl中的alarm、eval、die的联合使用
- TabLayout+Fragment+ViewPager联合使用
- 联合使用抽象类和接口
- AlarmManager的使用
- 9、SQL Server 使用联接和联合合并数据
- Mysql Union 联合查询的使用