- 浏览: 82342 次
- 性别:
- 来自: 成都
最新评论
-
284772894:
好文章,谢谢
android硬件传感器 -
康妮西:
求这个View的解释望博主联系,我的联系方式:kangnixi ...
让scrollview在滚动的过程中自动定位页的边边 -
muyu114:
你这个是啥意思啊,有什么效果没有
让scrollview在滚动的过程中自动定位页的边边
Service Broadcast BroadcastReceiver AlarmManager,Notification利用service定时/周期性发送广播
- 博客分类:
- Android
1.如题
主要实现
1.启动service,关闭service;
2.activity 向service发送广播。
3.service向 activity发送广播。
4.service 利用AliarmMnager定时向activity push 数据,其实也是发了个广播。
5.service 利用AliarmMnager周期性的向 activity push 数据,其实也是发了个广播
6.定时/周期性向通知栏推送消息
1.Demo.java
2.MyService.java
3.AndroidManifest.xml
主要实现
1.启动service,关闭service;
2.activity 向service发送广播。
3.service向 activity发送广播。
4.service 利用AliarmMnager定时向activity push 数据,其实也是发了个广播。
5.service 利用AliarmMnager周期性的向 activity push 数据,其实也是发了个广播
6.定时/周期性向通知栏推送消息
1.Demo.java
package aliusa.cn; import android.app.Activity; /** * The Class Demo. * @author aliusa */ public class Demo extends Activity{ TextView textView; Button[] btn; MyReceiver receiver = new MyReceiver(); static final String ACTION = "aliusa.cn.Demo"; String serviceState = "MyService is not Running."; String requireData = "The data is"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); final LinearLayout layout = new LinearLayout(this); layout.setOrientation(LinearLayout.VERTICAL); layout.setGravity(Gravity.CENTER); textView = new TextView(this); textView.setText(serviceState + requireData); layout.addView(textView); btn = new Button[6]; btn[0] = new Button(this); btn[1] = new Button(this); btn[2] = new Button(this); btn[3] = new Button(this); btn[4] = new Button(this); btn[5] = new Button(this); btn[0].setText("StopService"); btn[1].setText("BackGround"); btn[2].setText("RequireData"); btn[3].setText("StartService"); btn[4].setText("RequireDataDelay 5s"); btn[5].setText("RequireDataRepeating 5s"); //1.activity communicate with service through broadcast btn[0].setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { stopService(); } }); btn[1].setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { finish(); } }); //2.activity communicate with service through service instance. btn[2].setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { MyService.getInstance().pushDataImmediately(); } }); btn[3].setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { startService(); } }); btn[4].setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { MyService.getInstance().pushDataDelay(); } }); btn[5].setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { MyService.getInstance().pushDataRepeating(); } }); layout.addView(btn[0]); layout.addView(btn[1]); layout.addView(btn[2]); layout.addView(btn[3]); layout.addView(btn[4]); layout.addView(btn[5]); setContentView(layout); startService(); } Handler myHandler = new Handler() { public void handleMessage(Message msg) { textView.setText(serviceState + requireData); super.handleMessage(msg); } }; public void startService(){ receiver = new MyReceiver(); IntentFilter filter = new IntentFilter(); filter.addAction(ACTION); registerReceiver(receiver, filter); Intent i = new Intent(this, MyService.class); startService(i); serviceState = "MyService is Running."; myHandler.sendEmptyMessage(0); } @Override protected void onDestroy() { unregisterReceiver(receiver); myHandler = null; super.onDestroy(); } public class MyReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { Bundle bundle = intent.getExtras(); int data = bundle.getInt("data"); requireData = "The data is "+data; myHandler.sendEmptyMessage(0); } } public void stopService(){ Intent intent = new Intent(); intent.setAction(MyService.ACTION); intent.putExtra("cmd", MyService.SERVICE_STOP); sendBroadcast(intent); serviceState = "MyService is not Running."; myHandler.sendEmptyMessage(0); } }
2.MyService.java
package aliusa.cn; import android.app.AlarmManager; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.IBinder; import android.os.SystemClock; import java.util.Calendar; import java.util.Random; /** * The Class MyService. * @author aliusa */ public class MyService extends Service{ static final int SERVICE_STOP = 0; static final int SERVICE_PUSH_DATA = 1; static final String ACTION= "aliusa.cn.MyService"; static MyService instance = null; MyReceiver recevier; NotificationManager nm; AlarmManager am; PendingIntent senderDelay; PendingIntent senderRepeating; public void registerReviver(){ recevier = new MyReceiver(); IntentFilter filter1 = new IntentFilter(); filter1.addAction(ACTION); registerReceiver(recevier, filter1); } class MyReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { int cmd = intent.getIntExtra("cmd", -1); switch (cmd) { case MyService.SERVICE_STOP: stopSelf(); break; case MyService.SERVICE_PUSH_DATA: pushDataImmediately(); break; } } } @Override public IBinder onBind(Intent arg0) { return null; } @Override public void onCreate() { instance = this; super.onCreate(); } @Override public void onStart(Intent intent, int startId) { registerReviver(); super.onStart(intent, startId); } /** * Push data. */ public void pushData(){ final Random random = new Random(); Intent i = new Intent(); i.setAction(Demo.ACTION); i.putExtra("data", random.nextInt(100)); sendBroadcast(i); } public void pushDataImmediately(){ showNotification(); pushData(); } private void showNotification() { nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Intent intent = new Intent(this, Demo.class); PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0); Notification myNotification = new Notification(); myNotification.icon = R.drawable.icon; myNotification.defaults = Notification.DEFAULT_ALL; myNotification.setLatestEventInfo(this, "MyService is running", "chose to have a look", pi); nm.notify(0, myNotification); } @Override public void onDestroy() { cancelAlarmManager(); if (nm != null){ nm.cancelAll(); } unregisterReceiver(recevier); System.out.println("MyService.onDestroy"); super.onDestroy(); } public void pushDataDelay() { Intent intent = new Intent(); intent.setAction(MyService.ACTION); intent.putExtra("cmd", MyService.SERVICE_PUSH_DATA); senderDelay = PendingIntent.getBroadcast(this, 0, intent, 0); //设定一个五秒后的时间 Calendar calendar=Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); calendar.add(Calendar.SECOND, 5); am =(AlarmManager)getSystemService(ALARM_SERVICE); am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), senderDelay); //或者以下面方式简化 //alarm.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+5*1000, senderDelay); } /** * Require data. */ public void pushDataRepeating() { Intent intent = new Intent(); intent.setAction(MyService.ACTION); intent.putExtra("cmd", MyService.SERVICE_PUSH_DATA); senderRepeating = PendingIntent.getBroadcast(this, 1, intent, 0); //开始时间 long firstime=SystemClock.elapsedRealtime(); am=(AlarmManager)getSystemService(ALARM_SERVICE); //5秒一个周期,不停的发送广播 am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstime, 5*1000, senderRepeating); } public void cancelAlarmManager(){ if (am != null) { if (senderDelay != null) { am.cancel(senderDelay); senderDelay = null; } if (senderRepeating != null) { am.cancel(senderRepeating); senderRepeating = null; } am = null; } } public static MyService getInstance() { return instance; } }
3.AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="aliusa.cn" android:versionCode="1" android:versionName="1.0"> <uses-permission android:name="android.permission.VIBRATE"></uses-permission> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Demo" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name="aliusa.cn.MyService"> </service> </application> <uses-sdk android:minSdkVersion="7" /> </manifest>
发表评论
-
Android_SDK_NDK_JNI
2014-09-10 12:26 685Android SDK、NDK、JNI的简 ... -
如何检查apk是否签名
2012-02-22 16:46 2442Use this command : (Jarsigner i ... -
切割bitmap
2011-11-15 16:09 936Bitmap resource = BitmapFactory ... -
TextView控件属性列表
2011-10-08 15:58 1132纯属转载: http://www.cnbl ... -
元数据Meta Data
2011-09-30 16:30 1103<meta-data andr ... -
Android UI Operation in Thread
2011-09-21 17:16 1061Android UI Operation in Thread ... -
系统自带样式Android:theme
2011-06-23 15:58 2194Android应用开发——系统自带样式Android:them ... -
EditText自动获取焦点并弹出输入法的问题
2011-06-16 16:25 49861.每次启动新的Activity的时候,输入法总是弹出来,太烦 ... -
android硬件传感器
2011-05-30 16:07 13297纯属转载:http://dev.10086 ... -
权限permission
2011-05-27 18:03 1231纯属于转载: http://blog.csdn.net/ ... -
读写系统设置——日期与时间使用网络提供的值(时间与网络同步)
2011-05-27 17:19 2501//读_1.带默认参数的 Settings.Syste ... -
读写系统设置
2011-05-27 17:13 1757Android的很多设置想关的东西都存储在数据库中 andro ... -
Display_获取屏幕宽高
2011-05-06 12:19 939public static void intScreen(A ... -
seletctAll___利用ScrollView+RadioGroup+RadioButton实现相似互斥
2011-05-03 17:58 2046假如你有多个匹配list,在这些匹配list中存在这样的一些匹 ... -
selectAll___CheckBox in ListView
2011-05-03 17:32 14131.如果你有一个ListView,listView的 adap ... -
Layout___模块重用之include布局整合
2011-04-06 14:04 1314在Android的开发中,我们知道布局文件可以让我们很 ... -
value-string
2011-04-06 13:36 2057我们知道在android的value文件夹下,string.x ... -
利用PorterDuff.Mode做橡皮擦效果
2011-04-01 18:03 10696http://stackoverflow.com/questi ... -
UI___tab view 的实现
2011-03-24 15:19 1925一.通过xml文件实现 1.创建一个tab iew,所需的x ... -
常用的html特殊符号转义符
2011-03-24 14:40 2187HTML特殊转义字符列表,最常用的字符实体 Character ...
相关推荐
android service Notification BroadcastReceiver Intent 安装的服务开机自启动,service定时的打印消息,消息同时显示在通知栏,窗体界面的跳转,自定义控件的开发。系统广播,定时广播,轮询广播等。...
Android Activity、Service、Broadcast、Notification四合一例子,Activity中点击按钮启动服务,服务里每4秒发送一次广播,广播接受者每接受到一个发送一条通知。
android广播实现代码,使用两种方法注册BroadcastReceiver
NULL 博文链接:https://wilian.iteye.com/blog/966475
利用广播Broadcast Receiver,在2个不同的Activity传递数据
第一个例子 博文链接:https://lsdev.iteye.com/blog/1027868
1. 通过 AlarmManager 来设置定制任务 2. 用广播去启动Service 3. Service中onStartCommand()方法执行任务
本示例 利用Broadcast Receiver 启动Service,在这里同时又启动了一个Activity用于提示服务启动成功
Service,Broadcast学习实例
BroadcastReceiver是全局|系统级别的监听器 Notification和NotificationManager是局部|程序级别的监听器,可以实现可视化的信息显示,通过它在设备状态栏上显示广播信息的内容以及图标和震动等信息
Android下BroadcastReceiver的简单示例。详见博客:http://www.cnblogs.com/plokmju/p/android_BroadcastReceiver.html
在Android开发中很少有activity service broadcast 单例的综合使用,我写的这个例子已经能运行了希望能和大家交流学习
// 60秒一个周期,不停的发送广播 // am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstime, // 60 * 1000, pi); // 这里改成了一秒 ,为了测试一下效果 am.setRepeating(AlarmManager....
Android创建Service后台常驻服务并使用Broadcast通信,即基于Broadcast通信方式实现了一种Service Thread与Activity异步工作的方法。
安卓四大组件——BroadcastReceiver(广播)
Broadcast广播的使用,简单介绍了静态、动态注册广播,以及接受系统发出的广播和自定义本地广播
主要完成手机背景的定时切换功能,根据你选择的图片完成每天背景的切换
Android利用广播BroadCast监听网络的变化。该Demo是网络资源整理,可以正常运行。你可以看到内部接口的使用,广播机制的使用,网络连接状态的检查等。
这样一个通知就发送成功了,通知栏上也会显示出SmallIcon图标,之后是进行点击通知进行下载处理,首先Notification发送一个Broadcast广播,但是广播里面不能做耗时操作,所以在广播里开启一个Service服务,通过在...
本项目将Android中的Intent总结了一遍。主要有四个项目,Intent_Intent项目是简单的Intent跳转...intent_service主要是启动服务来播放音乐(我今年正好十八岁-阿正)。 本项目使用Android Studio 1.4 编写。