开机自启动和由一个APK启动另一个APK核心代码

1、开机自启动

  1. public class BootReceiver extends BroadcastReceiver {  
  2.   
  3.     private static final String ACTION = "android.intent.action.BOOT_COMPLETED";  
  4.     private static final String TAG = "ZED_Led_Receiver";  
  5.     private static final String FileName = "/sys/class/zed/led1";  
  6.     private static final String action_boot="android.intent.action.BOOT_COMPLETED"//启动APK  
  7.   
  8.   
  9.     @Override  
  10.     public void onReceive(Context context, Intent intent) {  
  11.         //Log.e(TAG,"LedReceiver.");  
  12.         if (intent.getAction().equals(ACTION)){  
  13.             {   //启动APK  
  14.                 Intent ootStartIntent=new Intent(context,BootActivity.class); //这一句的BootActivity是要启动的activity  
  15.                 ootStartIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   
  16.                 context.startActivity(ootStartIntent);   
  17.             }  
  18.             if(android.os.SystemProperties.get("persist.sys.skled").equals("1")){  
  19.                 ledSet(1);  
  20.             }  
  21.             else{  
  22.                 ledSet(0);  
  23.             }   
  24.         }     
  25.     }  

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.pstreets.demo"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk android:minSdkVersion="10" />  
  8.     <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />  
  9.     <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />  
  10.    
  11.     <application  
  12.         android:icon="@drawable/ic_launcher"  
  13.         android:label="@string/app_name" >  
  14.         <activity  
  15.             android:name=".BootupDemoActivity"  
  16.             android:label="@string/app_name" >  
  17.             <intent-filter>  
  18.                 <action android:name="android.intent.action.MAIN" />  
  19.   
  20.                 <category android:name="android.intent.category.LAUNCHER" />  
  21.             </intent-filter>  
  22.         </activity>  
  23.           
  24.          <receiver android:name=".BootupReceiver" android:enabled="true"  
  25.           android:permission="android.permission.RECEIVE_BOOT_COMPLETED">              
  26.             <intent-filter>                  
  27.                 <action android:name="android.intent.action.BOOT_COMPLETED"></action>     
  28.                  <category android:name="android.intent.category.DEFAULT" />           
  29.             </intent-filter>   
  30.   
  31.         </receiver>  
  32.     </application>  
  33.   
  34. </manifest>  

2、一个APK启动另一个APK代码
  1. package com.motic.uitest;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.ComponentName;  
  5. import android.content.Intent;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10.   
  11. public class UITestActivity extends Activity {  
  12.     /** Called when the activity is first created. */  
  13.     Button m_btn;  
  14.     @Override  
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.main);  
  18.           
  19.         m_btn = (Button)findViewById(R.id.but);  
  20.         m_btn.setOnClickListener(new BtnLister());        
  21.     }  
  22.       
  23.       
  24.     public class BtnLister implements OnClickListener  
  25.     {  
  26.         public void onClick(View v) {  
  27.               
  28.             Intent mIntent = new Intent( );   
  29.             ComponentName comp = new ComponentName("com.example""com.example.UITutorial_V2Activity");  
  30. //          ComponentName comp = new ComponentName("com.example.layout",  
  31. //                  "com.example.layout.SmsActivity");  
  32.             mIntent.setComponent(comp);   
  33.             mIntent.setAction("android.intent.action.VIEW");   
  34.             startActivity(mIntent);  
  35.             finish();  
  36.         }         
  37.     }  
  38.       
  39.     

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值