启动后立即自动启动应用程序

时间:2014-06-17 11:49:39

标签: android boot autostart

我想在手机启动时启动我的应用程序

我只是关注here的教程,但它在我的设备中无效。请看我的方法:

package net.londatiga.android;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class MyBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent startServiceIntent = new Intent(context, ExampleActivity.class);
        context.startService(startServiceIntent);
    } 
}

这是我的明白

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="net.londatiga.android"
      android:versionCode="2" android:versionName="1.01">
    <uses-sdk android:minSdkVersion="7" 
        android:targetSdkVersion="15"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <application android:icon="@drawable/icon" android:label="@string/app_name">

         <receiver android:name="net.londatiga.android.MyBroadcastReceiver"  
     android:enabled="true"
    android:exported="true">
    <intent-filter android:priority="1000">
        <action android:name="android.intent.action.BOOT_COMPLETED" />  
    </intent-filter>  
</receiver>

        <activity android:name=".ExampleActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


    </application>

</manifest>

我的错误在哪里?

3 个答案:

答案 0 :(得分:1)

而不是:

 context.startService(startServiceIntent);

使用:

 context.startActivity(startServiceIntent);

答案 1 :(得分:0)

您没有任何服务,您需要打开活动。

Intent startServiceIntent = new Intent(context, ExampleActivity.class);

context.startActivity(startServiceIntent);

答案 2 :(得分:0)

将其命名为 AfterBootActivity

  public class AfterBootActivity extends Activity {
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
}}

现在,创建一个名为 Autostart.java 的新类,它扩展了 BroadcastReceiver

public class Autostart extends BroadcastReceiver {
   public void onReceive(Context context, Intent arg1) {          
        Intent intent = new Intent(context, StarterService.class);
        context.startService(intent);
}}

在Manifest文件中,将此类添加为接收者。该类将在启动序列完成后,即在电话启动后收听Android操作系统发送的广播呼叫。

现在创建一个名为 StarterService.java 的类,它将扩展Service:

 public class StarterService extends Service {
   private static final String TAG = "MyService";
   public IBinder onBind(Intent intent) {
    return null;
}

public void onDestroy() {
    Toast.makeText(this, "Service stopped", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onDestroy");
}

/**
 * The below started service opens the Activity.
 */

public void onStart(Intent intent, int startid) {
    Intent intents = new Intent(getBaseContext(), AfterBootActivity.class);
    intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intents);

    Toast.makeText(this, "Service started", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onStart");
}}

当Autostart类从Android OS收到BOOT_COMPLETED广播时,它将启动StarterService,然后启动Android Activity“AfterBootActivity”,即我们的主类。我们可以播放任何音频/视频或任何内容。

更改 Manifest.xml ,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="on.boot.completed"
    android:installLocation="internalOnly"
    android:versionCode="1"
    android:versionName="1.0" >

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

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name="on.boot.completed.AfterBootActivity"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name="on.boot.completed.Autostart" >
            <intent-filter>
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

        <service
            android:name="on.boot.completed.StarterService"
            android:enabled="true"
            android:exported="true" />
    </application>

</manifest>

另外请记住将其安装在内部存储器中,因为如果安装在SD卡上的应用程序然后自动启动将无效!这就是我们添加清单的重要原因。

android:installLocation="internalOnly"

这就是你的应用程序。 启动后,关闭手机并重新打开,设备启动后应用程序会自动启动。

相关问题