zoukankan      html  css  js  c++  java
  • Android 广播(Broadcast)程序开机自启动

    学习这个“通过广播开机自启动”知识点时忽略了手机本身“设置”里面自启动权限的没有打开,害得我思考好久。

    BootTestActivity.class

    package com.xxx.study;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    
    public class BootTestActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
        }
        
    }

    BootCompletedReceiver.class

    package com.xxx.study; 
     
    import android.R.anim;
    import android.content.BroadcastReceiver; 
    import android.content.Context; 
    import android.content.Intent; 
    import android.text.AndroidCharacter;
     
    public class BootCompletedReceiver extends BroadcastReceiver { 
      @Override 
      public void onReceive(Context context, Intent intent) { 
            System.out.println("@@@@@@");
            
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) 
        { 
            System.out.println("@@@@@@...bootComplete!");
          Intent newIntent = new Intent(context, BootTestActivity.class); 
          newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  //注意,必须添加这个标记,否则启动会失败 
          context.startActivity(newIntent);  
          
        }       
      } 
    } 
    manifest.xml
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.xxx.study"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="18" />
     <!-- permissions --> 
        <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.xxx.study.BootTestActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
              <!-- receivers --> 
            <receiver android:name=".BootCompletedReceiver"> 
                <intent-filter> 
                    <action android:name="android.intent.action.BOOT_COMPLETED" /> 
                     <category android:name="android.intent.category.LAUNCHER" />  
                </intent-filter> 
            </receiver> 
        </application>
    
    </manifest>
                              作者:xubuhang                出处:http://www.cnblogs.com/xubuhang/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 

     
查看全文
  • 相关阅读:
    OD调试1--第一个win32程序
    Koa与Node.js开发实战(1)——Koa安装搭建(视频演示)
    《11招玩转网络安全》之第五招:DVWA命令注入
    《11招玩转网络安全》之第四招:low级别的DVWA SQL注入
    一张图11招学会Python网络黑客
    《11招玩转网络安全》之第三招:Web暴力破解-Low级别
    《11招玩转网络安全》之第二招:漏洞扫描
    《11招玩转网络安全》之第一招:Docker For Docker
    11招玩转黑客攻防——用Python,更安全
    如何有效的练习并且提升写代码的能力?
  • 原文地址:https://www.cnblogs.com/xubuhang/p/4206384.html
  • Copyright © 2011-2022 走看看