Android笔记二十九.Service组件入门(三).使用IntentService
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Android笔记二十九.Service组件入门(三).使用IntentService,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4935字,纯文字阅读大概需要8分钟。
内容图文
![Android笔记二十九.Service组件入门(三).使用IntentService](/upload/InfoBanner/zyjiaocheng/1120/6c773180fa03478c9ad65fc1691c2165.jpg)
![技术分享](/upload/getfiles/default/2022/11/15/20221115103904615.jpg)
![技术分享](/upload/getfiles/default/2022/11/15/20221115103904746.jpg)
package com.example.android_intentservice; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; public class ServiceAndIntentService extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } //1.启动普通Service按钮方法 public void startService(View source) { Intent intent = new Intent(this,MyService.class); //创建需要启动的service的Intent startService(intent); //启动Intent指定的Service } //2.启动IntentService按钮方法 public void startIntentService(View source) { Intent intent = new Intent(this,MyIntentService.class); startService(intent); } }
...... <Button android:onClick="startService" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="启动普通Service" /> <Button android:onClick="startIntentService" android:layout_width="186dp" android:layout_height="wrap_content" android:text="启动IntentService" />
package com.example.android_intentservice; import android.app.Service; import android.content.Intent; import android.os.IBinder; public class MyService extends Service { @Override public IBinder onBind(Intent intent) { return null; } //当启动Service,调用该方法执行相应代码 @Override public int onStartCommand(Intent intent, int flags, int startId) { long endTime = System.currentTimeMillis()+20*1000; System.out.println("onStart"); while(System.currentTimeMillis()<endTime) { synchronized(this) { try { wait(endTime-System.currentTimeMillis()); }catch(Exception e) { } } } System.out.println("---普通Service耗时任务执行完成---"); return START_STICKY; } } (4)\src\com\example\android_intentservice\MyIntentService.java 实现:实现一个IntentService执行耗时任务(20s),观察是否导致ANR异常 package com.example.android_intentservice; import android.app.IntentService; import android.content.Intent; public class MyIntentService extends IntentService { public MyIntentService() { super("MyIntentService"); } //IntentService会使用单独的线程来执行方法的代码 @Override protected void onHandleIntent(Intent intent) { //该方法内可以执行任何耗时任务,比如下载文件等,此处只是让线程暂停20s long endTime = System.currentTimeMillis()+20*1000; System.out.println("onStart"); while(System.currentTimeMillis()<endTime) { synchronized(this) { try { wait(endTime-System.currentTimeMillis()); }catch(Exception e) { } } } System.out.println("---IntentService耗时任务执行完成---"); } }
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity .......... </activity> <!--配置service--> <service android:name=".MyService"/> <service android:name=".MyIntentService"/> </application>
![技术分享](/upload/getfiles/default/2022/11/15/20221115103904990.jpg)
原文:http://blog.csdn.net/u012637501/article/details/44537077
内容总结
以上是互联网集市为您收集整理的Android笔记二十九.Service组件入门(三).使用IntentService全部内容,希望文章能够帮你解决Android笔记二十九.Service组件入门(三).使用IntentService所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。