Android中如何做到Service被关闭后又自动启动

Posted by & filed under Android.

首先要说的是,用户可能把这种做法视为流氓软件。大部分时候,程序员也不想把软件做成流氓软件,没办法,领导说了算。

 

我们在使用某些Android应用的时候,可能会发现安装了某应用以后,会有一些服务也会随之运行。而且,这些服务每次都会随着手机开机而启动。有的服务做的更绝,当用户在运行的服务中手动停止该服务以后,过了一段时间,服务又自动运行了。虽然,从用户的角度来说,这种方式比较流氓。但是,从程序员的角度来说,这是如何做到的呢?经过研究,我发现有一种方式是可以实现的。下面就和大家分享。

 

先简单介绍,一会儿会贴上全部代码。

 

如何做到开机启动?

 

这个比较简单,网上的资料够多,只要实现一个BroadcastReceiver,监听手机启动完成的事件ACTION_BOOT_COMPLETED即可。需要注意的是,好像不能用模拟器,要用手机测试。

 

那如何做到启动一个Service,并且在用户关闭后能自动又启动了呢?

 

一般的,都会在上面说到的BroadcastReceiver的实现里面,监听手机启动完成后,启动一个Service,这是一般的做法。问题是,用户可以关闭掉该Service。那么怎样才能使它被关闭掉以后,再次启动呢?聪明的你一定立即就想到了,如果不直接启动Service,而是启动一个timmer,或者alarmManager,然后每隔一段时间去启动Service,就可以了。

 

 

还是看下面的全部代码吧,不过多解释了。这些代码中还是有不少概念的,不熟悉AlarmManager、PendingIntent、BroadcastReceiver、Service等等这些概念的同学可以百度一下。

 

  1. package com.arui.framework.android.daemonservice;  
  2.   
  3. import android.app.AlarmManager;  
  4. import android.app.PendingIntent;  
  5. import android.content.BroadcastReceiver;  
  6. import android.content.Context;  
  7. import android.content.Intent;  
  8. import android.os.SystemClock;  
  9.   
  10. public class BootBroadcast extends BroadcastReceiver {  
  11.   

Android中Context详解 —- 你所不知道的Context

Posted by & filed under Android.

             前言:本文是我读《Android内核剖析》第7章 后形成的读书笔记 ,在此向欲了解Android框架的书籍推荐此书。

          大家好,  今天给大家介绍下我们在应用开发中最熟悉而陌生的朋友—–Context类 ,说它熟悉,是应为我们在开发中

   时刻的在与它打交道,例如:Service、BroadcastReceiver、Activity等都会利用到Context的相关方法 ; 说它陌生,完全是

   因为我们真正的不懂Context的原理、类结构关系。一个简单的问题是,一个应用程序App中存在多少个Context实例对象呢?

   一个、两个? 在此先卖个关子吧。读了本文,相信您会豁然开朗的 。

  

      Context,中文直译为“上下文”,SDK中对其说明如下:

         Interface to global information about an application environment. This is

深入理解ANDROID消息处理系统——LOOPER、HANDLER、THREAD

Posted by & filed under Android, Java.

(自) Activity,Service属于主线程,在主线程中才能更新UI,如toast等。其他线程中不能直接使用,这时可以使用Handler来处理,Handler可以在Activity和Service中。

关于在非UI线程中进行UI操作会出现问题: Can’t create handler inside thread that has not called Looper.prepare()

这时有两种方式来解决:

(一)在该非UI线程中创建消息队列(因为创建的工作线程默认是没有消息循环和消息队列的),Looper.prepare();…..;Looper.loop(); 

 newThread() { 

public void run() {

 Looper.prepare(); //创建消息队列

 todo(); 

 Looper.loop();//进入消息循环

}}.start();

(二)运用Handler机制:

package com.simon;

import android.app.Activity; 
import android.os.Bundle; 
import android.os.Message; 
import android.util.Log; …

Java Tip 10: Implement callback routines in Java

Posted by & filed under Java.

This story appeared on JavaWorld at

http://www.javaworld.com/javatips/jw-javatip10.html

Java Tip 10: Implement callback routines in Java

Using interfaces to implement the equivalent<BR> of callback functions in Java

Developers conversant in the event-driven programming model of MS-Windows and the

Cursors in SQL

Posted by & filed under DATABASE, ORACLE, SQLSERVER.

SQL was designed as a set-oriented processing language. Some business rules( or poor physical design) require performing actions on row-by-row basis. Consider the following example:
. Increase the price of books <=$15 by 15%
. Decrease the price of books …

Basic Rules of Normalization

Posted by & filed under DATABASE, ORACLE, SQLSERVER.

Without going into a dissertation on data modeling(itself a subject of many full-length treatises), tables should follow basic rules of normalization:

Avoid duplicate data, avoid repeating groups in tables, and only put data in tables where the information is directly

The logical processing of the Select statement

Posted by & filed under SQLSERVER.

CREATE TABLE Loans (
 loan_nbr INT NOT NULL ,
 customer_nbr INT NOT NULL,
 loan_date DATETIME NOT NULL,�
 loan_amount DECIMAL(15, 2) NOT NULL,
 loan_type CHAR(1) NOT NULL,
 CONSTRAINT ck_loan_type
 CHECK (loan_type IN(‘P’,’B’)), — P = Personal; –B=Business
 CONSTRAINT pk_loans
 PRIMARY KEY(loan_nbr));…

SQL Server中行列转换 Pivot UnPivot

Posted by & filed under SQLSERVER.

PIVOT用于将列值旋转为列名(即行转列),在SQL Server 2000可以用聚合函数配合CASE语句实现

PIVOT的一般语法是:PIVOT(聚合函数(列) FOR 列 in (…) )AS P

完整语法:

table_source

PIVOT(

聚合函数(value_column)

FOR pivot_column

IN(<column_list>)

)

 

UNPIVOT用于将列明转为列值(即列转行),在SQL Server 2000可以用UNION来实现

完整语法:

table_source

UNPIVOT(

value_column

FOR pivot_column

IN(<column_list>)

)

 

注意:PIVOT、UNPIVOT是SQL Server 2005 的语法,使用需修改数据库兼容级别
 在数据库属性->选项->兼容级别改为   90

 

典型实例

一、行转列

旧版本的Crystal Reports升级到Crystal Reports forVisual Studio 2010时的错误解决方法

Posted by & filed under Visual Studio.

我在使用BOOK: Professional Crystal Reports for VS.NET 的时候,经常出错:
…..Assembles不能用。。。。

这里最好的解决办法是,1.重新安装.Net FrameWork,2. Select the Compile tab and click on the “Advanced Compile Options…” button. Find “Target CPU” and set it to x86. Next find “Target Framework” and set it to .NET Framework …