android开机事件会发送一个叫做Android.intent.action.BOOT_COMPLETED的广播信息,我的程序会在接收到这个监听的时候开启我的应用.
代码如下:开启自启的类public class Hello extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}
接收Android.intent.action.BOOT_COMPLETED的广播信息的类
public class StartupReceiver extends BroadcastReceiver { @Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent i = new Intent(context,Hello.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//将intent以startActivity传送给操作系统
context.startActivity(i); }}
AndroidManifest.xml<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.practice.autostart"
    android:versionCode="1"
    android:versionName="1.0" >    <uses-sdk android:minSdkVersion="7" />    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />    <application
        android:icon="@drawable/icon"
        android:label="@string/app_name" >
        <activity
            android:name=".AutoStartActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>        <receiver android:name=".AutoStartBroadcast" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
    </application></manifest>问题:
我桌子上有4台机器
1.三星5660 android版本2.2.1     没有效果
2.山寨三星xxx android版本2.3.3  有效果
3.山寨HTC xx  android版本 2.3.4 有效果
4.htcXXX android版本2.3.3       没有效果....
请问这开机自启的代码 和android版本有关系没?或者跟机器设置有关系没?
有没有其他开机自启的方法?360肯定不是用的此方法吧...任何机器都能自动启动

解决方案 »

  1.   

    <receiver android:name=".AutoStartBroadcast" >
                <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED" />
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
    </receiver>看看这样成吗?
      

  2.   

    你在广播里输出一下log,未必是接收不到广播,或者只是启动不了Activity,
    另外,一些安全软件,360卫士什么的可能会阻止你的自启动
      

  3.   


    嗯...试试...
    能够解释一下 <category android:name="android.intent.category.DEFAULT" /> 的意思么?
      

  4.   

    我找到问题了....
    刚才不支持开机启动的机器,我如果把程序装在手机上面 就可以接收到广播和开启程序了....
    但是我装在sdcard就不行,连广播都接收不到...
    我去查一下资料...如果有经验的请帮组小弟一下呀....
      

  5.   

    什么叫“山寨三星”“山寨HTC”
    还是想说:山寨机,就是牛!
      

  6.   

    非官方公司出的三星和htc机器 就是山寨机器.... 只是外形和名字一样,配置完全不一样的手机..
      

  7.   

    与版本无关,开机启动相当简单
    只需要一个 activity 与一个broadcasrReceiver类
    就可以在运行一次以后,每次都开机启动直到删除这个程序为止
    xml中添这么几句话,委托receiver
    <receiver android:name="myTestReceiver" >
    <intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED" />
    <category android:anme="android.intent.category.HOME" />
    </intend-filter>
    连RECEIVE_BOOT_COMPLETED权限都不需设置
    //当然以上的receiver除了xml定义外,你也可以用这样的方式代码中定义
    registerReceiver(BroadcastReceiver,IntentFilter)
    两种方法一样的
      

  8.   


    往往看着简单的东西,都有它复杂的地方...
    android机型和厂商很多,以上代码不支持所有...
    默认装载sdcard里面的应用是监听不到开启的广播的...虽然你可以设置你装应用的位置,但是也只有2.2或者以上的android版本才支持....
    ...
    现在的问题只有1个了..HTC Salsa C510e 这台机器,连应用装在手机内存上都无法监听到开机的广播...
      

  9.   

    sdcard的装载有可能在系统广播发出后才完成,如果你的程序装在sdcard里面,就有可能来不及收到广播。有个偏方:可以设置一些经常会触发的广播接收器,在里面完成启动。例如:如果需要网络的程序,就可以接收网络状态改变的广播
      

  10.   


    我的修改方法是 在xml里面设置 只装载到手机内存..本身项目比较小,所以此方法可行....
    但是g14的机器,貌似关机被厂商修改了,不是正常的关机,是貌似休眠的处理,所以开机不会广播开机的广播,可以根据你说的去单独满足htc的g系列机器.....