码迷,mamicode.com
首页 > 移动开发 > 详细

只有一个Service或Broadcast Reciver的android应用

时间:2014-11-25 12:24:36      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   io   ar   color   os   使用   

Service是android四大组件中与Activity最相似的组件,都可以代表可执行的程序。
Service与Activity的区别在于:
  (1)、Service一直在后台运行,没有用户界面。
  (2)、一旦service被启动之后,就跟Activity一样。有自己的生命周期。所以可以没有Activity。
开发service需要两个步骤:
   (1)、定义一个继承service的子类
   (2)、在AndroidManifest.xml中配置该Service ,其过程和配置Activity一样。
Service运行有两种方式:
    【1】、通过Context的startService()方法,通过该方法启动用Service,访问者与service之间没有关联,即使访问者退出了,Service仍然运行。
    【2】、通过Context的bingSerive()方法,使用该方法启用Service,访问者和service形成关联,即绑定在一起,访问退出,Service也退出。
而Broadcast Reciver本质是一种全局的监听器,它可以用来组件之间相互通信。它用来接收程序所发出的Broadcast intent,与应用启动Activity,service相同的是:程序启动Broadcast Reciver也是需要两个步骤
    【1】、创建Broadcast Reciver的Intent
    【2】、调用context的sendBroadcase()或者sendorderBroadcase()方法来启动制定的BroadcastReciver
    在笔者下面所演示的代码中,将这个service和Broadcast Reciver结合在一起,可以不需要activity。当程序接收一个Broadcast Reciver的时候,就启动service(service也可以通过activity来启动)。这个例子就是开机自己启动服务。开机的时候会进行广播,我们就将这个广播进行接收,然后开启服务!
Broadcast程序代码:
package com.keenhi.tianpei;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class test_chargeReceive extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
        Intent intent1 = new Intent(context ,test_chargeService.class);  
        
        context.startService(intent1); 
}

}
Service程序代码:
package com.keenhi.tianpei;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class test_chargeService extends Service {
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate()  
{
 System.out.println("service create");
}
}

Manifest.xml程序代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.keenhi.tianpei"
    android:versionCode="1"
    android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".Test_chageActivity"
            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="test_chargeService"></service>
        <receiver android:name="test_chargeReceive">
             <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>
    </application>
</manifest>
完成上面的程序就ok了,如果你是直接把xxx.apk 用adb  push进system/app 这绝对是没问题的,程序会自启动。但是如果在eclipse做测试,我发现如果不启动一次activity的话,BroadcastReceiver 接收不到自启action,原因不清楚,估计是android为了防止木马后台运行而又从来没有提示过用户吧。
 
无用的Activity程序代码:
package com.keenhi.tianpei;
import android.app.Activity;
import android.os.Bundle;
public class Test_chageActivity extends Activity {
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);  
    }
}
大家可以看出,这个activity是绝对没有用的,只会在eclipse加载程序进机台的时候打开一次,以后你经过无数次关机,他再不会调用,但service会通过BroadcastReceiver起来。不过测试的时候一定要有activity,切记。 测试好了后的xxx.apk我们可以把activity去掉,直接push进system/app程序完好后台运行。
因为有service所以我们可以通过进程控制器(设置/应用程序),看到后台运行的进程和服务。
但是其实我们可以不要service,只有一个BroadcastReceiver就可以了。但这时请注意,此时没有service和进程,我们在进程控制器是查不到了,但它确实运行了。它会把自己做为系统程序的一部分加载。 测试的时候仍然需要建立一个activity,push进system/app则只需要一个BroadcastReceiver就足够了。
一个BroadcastReceiver的程序和上面一样,直接把service去掉就可以了。
另外一般这样的程序都会做成和系统一样的守护进程,让它不能被kill掉。方法如下:
 
在manifest.xml文件下添加:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
       android:sharedUserId="android.uid.system">
<application android:icon="@drawable/icon" android:label="@string/app_name"
                android:allowClearUserData="false" android:process="system"
                android:killAfterRestore="false">

 

 

转自:http://blog.sina.com.cn/s/blog_76550fd701017qej.html

只有一个Service或Broadcast Reciver的android应用

标签:android   style   blog   http   io   ar   color   os   使用   

原文地址:http://www.cnblogs.com/welcoming/p/4120497.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!