在启动时启动应用程序

时间:2012-02-25 07:41:02

标签: java android autostart

我希望我的应用程序在系统启动时自动启动。谁能告诉我怎么做?

提前感谢你花时间去帮助我。

2 个答案:

答案 0 :(得分:5)

首先,您需要清单中的权限:

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

此外,在您的清单中,定义您的服务并监听启动完成的操作:

<service android:name=".MyService" android:label="My Service">
    <intent-filter>
        <action android:name="com.myapp.MyService" />
    </intent-filter>
</service>

<receiver
    android:name=".receiver.StartMyServiceAtBootReceiver"
    android:enabled="true"
    android:exported="true"
    android:label="StartMyServiceAtBootReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

然后你需要定义接收器,它将获得BOOT_COMPLETED动作并启动你的服务。

public class StartMyServiceAtBootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
            Intent serviceIntent = new Intent("com.myapp.MySystemService");
            context.startService(serviceIntent);
        }
    }
}

现在,您的服务应该在手机启动时运行。

答案 1 :(得分:0)

这是完整的工作示例代码,

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.practice" android:versionCode="1" android:versionName="1.0">
    <uses-sdk android:minSdkVersion="4" />

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

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".AutoStartExampleActivity"
            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="AutoStart"></receiver>

        <receiver android:name=".AutoStart">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>


    </application>
</manifest>

AutoStartExampleActivity文件

package com.practice;

import android.app.Activity;
import android.os.Bundle;

public class AutoStartExampleActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

BroadcastReceiver Code,

package com.practice;

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

public class AutoStart extends BroadcastReceiver
{

    public void onReceive(Context context, Intent intent)
    {
        System.out.println ( "Application Started" );
        // put your TimerTask calling class here

        try
        {
            Intent myIntent = new Intent ( context, AutoStartExampleActivity.class );
            myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(myIntent);
        }
        catch ( Exception e )
        {
            System.out.println ( " Error while Starting Activity " + e.toString() );
        }
    }
}