android 程序后台运行,定时刷新,像qq那样,即使程序不启动也照样运行

来源:互联网 发布:淘宝县级服务站 编辑:程序博客网 时间:2024/05/06 07:56

开发过程所遇到的问题

1、android如何能够在后台偷偷的运行


实现代码


<pre name="code" class="java">IntentFilter filter = new IntentFilter(Intent.ACTION_TIME_TICK);         MyBroadcastReceiver receiver = new MyBroadcastReceiver();         acquireWakeLock();//获取电源锁        registerReceiver(receiver, filter);//注册广播   

以上代码放到主activity的onresume中


其中MyBroadcastReceiver的代码如下

//自定义广播public class MyBroadcastReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {/** * 在众多的Intent的action动作中,Intent.ACTION_TIME_TICK是比较特殊的一个, * 根据SDK描述:Broadcast Action: The current time has changed.  * Sent every minute. You can not receive this through  * components declared in manifests, only by exlicitly  * registering for it withContext.registerReceiver() * 意思是说这个广播动作是以每分钟一次的形式发送。但你不能通过在manifest.xml * 里注册的方式接收到这个广播,只能在代码里通过registerReceiver()方法注册。 */if (intent.getAction().equals(Intent.ACTION_TIME_TICK)) {// 检查Service状态boolean isServiceRunning = false;ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {if ("com.text.ac.action.MY_SERVICE".equals(service.service.getClassName()))// Service的类名{isServiceRunning = true;}}if (!isServiceRunning) {Intent i = new Intent(context, MessageService.class);context.startService(i);}}}}
这样就可以使你的程序在后台悄悄的运行了,每分钟监控一次服务是否启动,以保证服务一直运行,这样就可以做其他的事情了

2、怎么才能在手机锁屏的情况也能继续后台运行我的android程序呢

保证程序不会在锁屏的情况被清理掉

实现代码


/**     * 获取电源锁,保持该服务在屏幕熄灭时仍然获取CPU时,保持运行     * WakeLock 类型以及说明:    PARTIAL_WAKE_LOCK:保持CPU 运转,屏幕和键盘灯有可能是关闭的。    SCREEN_DIM_WAKE_LOCK:保持CPU 运转,允许保持屏幕显示但有可能是灰的,允许关闭键盘灯    SCREEN_BRIGHT_WAKE_LOCK:保持CPU 运转,允许保持屏幕高亮显示,允许关闭键盘灯    FULL_WAKE_LOCK:保持CPU 运转,保持屏幕高亮显示,键盘灯也保持亮度    ACQUIRE_CAUSES_WAKEUP:强制使屏幕亮起,这种锁主要针对一些必须通知用户的操作.    ON_AFTER_RELEASE:当锁被释放时,保持屏幕亮起一段时间    WakeLock的设置是 Activiy 级别的,不是针对整个Application应用的    权限获取    <uses-permission android:name="android.permission.WAKE_LOCK" />(使用找一个即可)你可能还需要<uses-permission android:name="android.permission.DEVICE_POWER" />     */     private void acquireWakeLock() {         if (null == wakeLock) {             PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);             wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK                     | PowerManager.ON_AFTER_RELEASE, getClass()                     .getCanonicalName());             if (null != wakeLock) {                 Log.i(TAG, "call acquireWakeLock");                 wakeLock.acquire();             }         }     }      // 释放设备电源锁      private void releaseWakeLock() {         if (null != wakeLock && wakeLock.isHeld()) {             Log.i(TAG, "call releaseWakeLock");             wakeLock.release();             wakeLock = null;         }     }

解释:获取手机的电源锁

释放电源锁在主activity中的onpause中

@Overrideprotected void onPause() {super.onPause();releaseWakeLock();}


service代码如下

public class MessageService extends Service {     //获取消息线程    private MessageThread messageThread = null;     //点击查看    private Intent messageIntent = null;    private PendingIntent messagePendingIntent = null;     //通知栏消息    private int messageNotificationID = 1000;    private Notification messageNotification = null;    private NotificationManager messageNotificatioManager = null;     public IBinder onBind(Intent intent) {        return null;    }         @Overridepublic void onCreate() {     //初始化        messageNotification = new Notification();        messageNotification.icon = R.drawable.icon;        messageNotification.tickerText = "新消息";        messageNotification.defaults = Notification.DEFAULT_SOUND;        messageNotificatioManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);        //点击跳转的activity        messageIntent = new Intent(this, ExTextActivity.class);        messagePendingIntent = PendingIntent.getActivity(this,0,messageIntent,0);             //开启线程        messageThread = new MessageThread();        messageThread.isRunning = true;        messageThread.start();        Toast.makeText(MessageService.this, "aaaa", Toast.LENGTH_LONG).show();super.onCreate();}    /**     * 重写onStartCommand方法,使用StartForeground(int,Notification)方法来启动service。      * 注:前台服务会在状态栏显示一个通知,最典型的应用就是音乐播放器,只要在播放状态下,     * 就算休眠也不会被杀,如果不想显示通知,只要把参数里的int设为0即可     * 小米手机需要设置允许自动启动;设置步骤:安全中心-授权管理-允许自动启动     * 其他类型手机,只要设置允许自动启动即可     */public void onStartCommand(){    Notification notification = new Notification(R.drawable.icon,"wf update service is running",System.currentTimeMillis());      messagePendingIntent=PendingIntent.getService(this, 0, messageIntent, 0);          notification.setLatestEventInfo(this, "WF Update Service","wf update service is running!", messagePendingIntent);          //让该service前台运行,避免手机休眠时系统自动杀掉该服务          //如果 id 为 0 ,那么状态栏的 notification 将不会显示。          startForeground(0, notification);     }/**     * 从服务器端获取消息     *     */    class MessageThread extends Thread{        //运行状态,下一步骤有大用        public boolean isRunning = true;        @SuppressWarnings("deprecation")public void run() {            while(isRunning){                try {                    //休息10分钟                Thread.sleep(5000);                    //获取服务器消息                    String serverMessage = getServerMessage();                                     if(serverMessage!=null&&!"".equals(serverMessage)){                    //此处获取要现实的数据                        //更新通知栏                        messageNotification.setLatestEventInfo(MessageService.this,"值班快报","新密市一煤矿发生坍塌事故。。。"+serverMessage,messagePendingIntent);                        messageNotificatioManager.notify(messageNotificationID, messageNotification);                        //每次通知完,通知ID递增一下,避免消息覆盖掉                       messageNotificationID++;                    }               } catch (InterruptedException e) {                    e.printStackTrace();                }            }        }    }@Overridepublic void onDestroy() {  //  System.exit(0);  //或者,二选一,推荐使用System.exit(0),这样进程退出的更干净  messageThread.isRunning = false;  super.onDestroy();}    /**     * 这里以此方法为服务器Demo,仅作示例    * @return 返回服务器要推送的消息,否则如果为空的话,不推送     */    public String getServerMessage(){        return "!";    }}


0 0