Jumat, 12 Maret 2010

Lanuch Application as Startup Application

In this article helps to lanuch your activity after the start your Phone.
1. Create a BroadcastReceiver Class and start your Main Activity
2. Set the User Permission for launch your application
3. Configure the Action and catgory in the intent-filter
4. Create a MainActivity

Create a BroadcastReceiver Class and start your Main Activity
    This BroadcastReceiver is used to receive the action and start the your main activity   

public class StartupActivity extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
   }
}


Set the User Permission for launch your application
Config the  " android.permission.RECEIVE_BOOT_COMPLETED " User Permission in the androidmanifest.xml

Configure the Action and catgory in the intent-filter
Set the following values for android and category

Action         :    android.intent.action.BOOT_COMPLETED
Category     :    android.intent.category.DEFAULT

Create a MainActivity
Create a MainActivity and write your logic there
public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
          }
}

Sample Code
MainActivity.java

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Toast.makeText(this, "Welcome to Application", Toast.LENGTH_LONG)
                .show();
        finish();
    }
}

BroadcastReceiver.java
public class StartupActivity extends BroadcastReceiver {
    /**
     * @see android.content.BroadcastReceiver#onReceive(Context,Intent)
     */
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent(context, MainActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }
}

Androidmanifest.xml
<?xml version="1.0" encoding="UTF-8"?>
<manifest android:versionCode="1" android:versionName="1.0"
    package="com.lanuchactivity" xmlns:android="http://schemas.android.com/apk/res/android">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:label="@string/app_name" android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".StartupActivity">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"></action>
                <category android:name="android.intent.category.DEFAULT"></category>
            </intent-filter>
        </receiver>
    </application>
    <uses-sdk android:minSdkVersion="7" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
</manifest>





Tidak ada komentar:

Posting Komentar