postheadericon Android Service Example




Ex9Activity.java

package com.bipin.rupadiya.Ex9;

import prg.Ex9.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class Ex9Activity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
    Intent intent = new Intent();
    intent.setClassName("prg.Ex9", "prg.Ex9.MyService");
   
   
    startService(intent);
       
        setContentView(R.layout.main);
    }
}



 MyServic.java

package com.bipin.rupadiya.Ex9;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class MyService extends Service
{
private Timer timer = new Timer();

@Override
public IBinder onBind(Intent arg0) {
return null;
}

@Override
public void onCreate() {
super.onCreate();
Log.i("MyService", "Service Created...");
}

@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.i("MyService", "Service Started...");
timer.schedule(new mainTask(), 1000 * 10);
}

private class mainTask extends TimerTask
{
public void run()
{
Intent in = new Intent(MyService.this, AlertActivity.class);
in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(in);
}
}

@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.i("MyService", "Service Destroyed...");
}
}



AlertActivity.java

package com.bipin.rupadiya.Ex9;


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

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

main.xml

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="36dp"
        android:layout_y="199dp"
        android:text="www.BipinRupadiya.com"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</AbsoluteLayout>




alert.xml

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="36dp"
        android:layout_y="199dp"
        android:text="welcome to new task"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</AbsoluteLayout>




AndroidManifest.xml

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

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Ex9Activity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name="com.bipin.rupadiya.Ex9.MyService"></service>
        <activity android:name="com.bipin.rupadiya.Ex9.AlertActivity"></activity>

    </application>
</manifest>


 

0 comments:

Follow by Email

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.