- 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
Service:后台服务 后台运行,几乎不与用户交互,没有可视化界面 与 Activity 一样,同属 Android 基本组件 同样需要在 AndroidManifest.xml 中注册 service android:name=.Runnable_Service android:exported=true android:process=:remote /service Service:后台服务 每一个服务均继承自父类 Service 同样以 startService() 方法启动服务 button1=(Button)findViewById(R.id.button1); button1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent intent1=new Intent(Runnable_Service_Test.this, Runnable_Service.class); intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startService(intent1); } }); 示例:Service on Android 实例:Test_of_Service private Runnable task1 = new Runnable() { public void run() { intCounter++; Log.i(test, int Counter: +Integer.toString(intCounter)); Toast.makeText(getApplicationContext(), Integer.toString(intCounter), Toast.LENGTH_SHORT) .show(); handler1.postDelayed(task1, 1000*second); } }; Runnable: 允许服务持续运行,即使启动服务的应用退出 类似于 定时器中断的中断服务程序 Broadcast Receiver:系统级别的事件处理机制 UI的事件处理机制:程序、组件级别的 sendBroadcast(Intent intent)方法 发送广播 BroadcastReceiver.onReceive()方法 接收广播并做相应处理 多个广播接收器可能同时接收同一个广播 Broadcast Receiver:广播接收器 标准的 Broadcast Action 常量: ACTION_TIME_CHANGED 时间改变 ACTION_DATE_CHANGED 日期改变 ACTION_BATTERY_LOW 电量不足 ACTION_MEDIA_EJECT 插拔外部媒体 ACTION_BOOT_COMPLETED 启动完成 …… 用户自定义广播事件 示例:Broadcast Receiver on Android 实例:Test_of_BroadcastReceiver_Send (1)Main Activity中定义一个 Button,其单击事件中准备数据并发起广播 private int i = 0; private static final String TESTACTION = test.broadcast.SEND; button1=(Button)findViewById(R.id.button1); button1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent intent1=new Intent(); intent1.setAction(TESTACTION); i++; intent1.putExtra(message,这是第 + i + 次广播……); sendBroadcast(intent1); } }); (2)定义继承自 BroadcastReceiver 类的Receiver1 类,用于接收广播并回显信息 import android.content.Context; import android.content.Intent; import android.widget.Toast; i
文档评论(0)