How to open my android app at a start up of android mobile? [duplicate]

邮差的信 提交于 2019-12-11 03:09:46

问题


Possible Duplicate:
How to Start an Application on Startup?

I am new to android and I have my android app. I just want to open my app at start up of my android mobile. Plz tell me How to do this? I am using Eclipse with android sdk 2.3.3


回答1:


If you want to do it pragmatically

<receiver android:name=".MyServiceManager"
        android:enabled="true" >

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

then just add this code into application tag of your manifest file and add usages permission into manifest file.

Then

public class MyServiceManager extends BroadcastReceiver
{

    @Override
    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();

        if(action.equalsIgnoreCase("android.intent.action.BOOT_COMPLETED"))
        {
            Intent myIntent=new Intent(context,package.YourActivity.class);
            myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(myIntent);
        }
}   

add this class into your project. Please check package name and class name and change it accordingly. If you still have problem then write me.




回答2:


Give permission in manifest.....

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

In your receiver give this to intent filter...

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

Try this code in receiver class.....

 @Override
    public void onReceive(Context context, Intent intent) {
        if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
            Intent serviceIntent = new Intent("*package name*.*Receivername*");
            context.startService(serviceIntent);
        }
    }

For more update about android visit this website.



来源:https://stackoverflow.com/questions/11079204/how-to-open-my-android-app-at-a-start-up-of-android-mobile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!