如何在移动重启或打开时启动我的应用程序

时间:2013-06-18 11:54:05

标签: android broadcastreceiver android-broadcast bootcompleted

如何将我的应用程序设置为启动应用程序,因此当移动设备重新启动或打开时,我的应用程序将启动。

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

 <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="8" />

 <application android:icon="@drawable/cherry_icon" android:label="@string/app_name">
    <activity android:name=".MainActivity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
<activity android:name=".ListInstalledApps" > </activity> 


<activity android:name=".TabsLayoutActivity" />
</application>
   </manifest>

编辑以下是我更新的代码,但仍无效:

清单:

 <?xml version="1.0" encoding="utf-8"?>

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.example.installedapps22"
android:versionCode="1"
 android:versionName="1.0" >

 <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="8" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
 <application android:icon="@drawable/cherry_icon" android:label="@string/app_name">
    <activity android:name=".MainActivity"
              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:enabled="true" android:name="com.app.reciever.BootUpReciever">
  <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <category android:name="android.intent.category.DEFAULT" />
  </intent-filter>
 </receiver>
  <activity android:name=".ListInstalledApps" > </activity> 


  <activity android:name=".TabsLayoutActivity" />
  </application>
 </manifest>

BroadcastReciever:

 package com.example.installedapps22; 
 public class BootUpReciever extends BroadcastReceiver
 {

    @Override
    public void onReceive(final Context context, Intent intent) {
        Intent i = new Intent(context, MainActivity.class);  
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }
 }

3 个答案:

答案 0 :(得分:4)

这是将应用程序设置为设备中的启动应用程序创建类扩展BroadCast接收器

public class BootUpReciever extends BroadcastReceiver
{

@Override
public void onReceive(final Context context, Intent intent) {
        Intent i = new Intent(context, ServerPreferenceActivity.class);  
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
}
}

添加清单文件的权限以访问启动接收器

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

注册扩展了manifest.xml中Broadcast receiver的接收者

<receiver android:enabled="true" android:name="com.app.reciever.BootUpReciever">
    <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>

答案 1 :(得分:1)

首先在清单中使用权限

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

并在清单

中声明启动接收器
<receiver android:name=".BootReciever">
    <intent-filter >
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
    </intent-filter>
</receiver>

用户接收器开始你的主动

public class BootReciever extends BroadcastReceiver
{

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    Intent myIntent = new Intent(context, MainActivity.class);
    myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(myIntent);
}

}

答案 2 :(得分:0)

对于您编辑的代码,请尝试替换以下内容:

android:name="com.app.reciever.BootUpReciever

使用:

android:name="com.example.installedapps22.BootUpReciever