zoukankan      html  css  js  c++  java
  • Android -- 开机启动无界面后台程序

    简单的说,这是一个没有界面的后台运行的应用程序

    而且,还有一功能:开机自启动,启动运行一个服务


    程序结构非常简单,两个类,一个是service的扩展类,一个是BroadcastReceiver扩展类


    1. import android.app.Service;  
    2. import android.content.Intent;  
    3. import android.os.IBinder;  
    4. import android.util.Log;  
    5. import android.widget.Toast;  
    6.   
    7. public class TestService extends Service{     
    8.   
    9.     public void onCreate(){     
    10.         super.onCreate();   
    11.         Log.d("AAAAAAAAAAAAAAAAAAA", "AAAAAAAAAAAAAAAAAAAAAAA");  
    12.         Toast.makeText(getApplicationContext(), "默认Toast样式",  
    13.               Toast.LENGTH_LONG).show();  
    14.     }     
    15.          
    16.     public IBinder onBind(Intent intent){     
    17.      Log.d("BBBBBBBBBBBBBBBBBBB", "BBBBBBBBBBBBBBBBBBBBBBB");   
    18.      Toast.makeText(getApplicationContext(), "不默认Toast样式",  
    19.              Toast.LENGTH_LONG).show();  
    20.         return null;     
    21.     }     
    22. }   

    BroadcastReceiver扩展类:
    1. import android.content.BroadcastReceiver;  
    2. import android.content.Context;  
    3. import android.content.Intent;  
    4. import android.util.Log;  
    5.   
    6. public class BootReceiver extends BroadcastReceiver {  
    7.   
    8.  @Override  
    9.  public void onReceive(Context arg0, Intent arg1) {  
    10.   // TODO Auto-generated method stub  
    11.   
    12.   Log.d("WWWWWWWWWWWWWWWWWWWWW", "WWWWWWWWWWWWWWWWWWWWWWWW");  
    13.   Intent mBootIntent = new Intent(arg0, TestService.class);  
    14.   arg0.startService(mBootIntent);  
    15.   Log.d("CCCCCCCCCCCCCCCCCCCCC", "CCCCCCCCCCCCCCCCCCCCCCCC");  
    16.  }  
    17. }  

    配置文件:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:Android="http://schemas.android.com/apk/res/android"
    package="com.tyq"
    android:versionCode="1"
    android:versionName="1.0" >

    <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <service android:name=".TestService"></service> 
    <receiver android:name=".BootReceiver">
    <intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
    </receiver>
    </application>

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-sdk android:minSdkVersion="10" />
    </manifest>

    参考http://netspook.iteye.com/blog/1291236

    附上android广播相关知识

    http://bk-lin.iteye.com/blog/1569568

     
     
  • 相关阅读:
    前端与算法 leetcode 344. 反转字符串
    JavaScript闭包使用姿势指南
    前端与算法 leetcode 48. 旋转图像
    前端与算法 leetcode 36. 有效的数独
    前端与算法 leetcode 1. 两数之和
    前端与算法 leetcode 283. 移动零
    前端与编译原理 用js去运行js代码 js2run
    前端与算法 leetcode 66. 加一
    前端与算法 leetcode 350. 两个数组的交集 II
    前端与算法 leetcode 26. 删除排序数组中的重复项
  • 原文地址:https://www.cnblogs.com/mafeng/p/6726017.html
Copyright © 2011-2022 走看看