Create background application that will open activity on specific time.

main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:layout_height="wrap_content"
android:id="@+id/button1" android:text="Start Service"
android:layout_width="match_parent"></Button>
<Button android:layout_height="wrap_content"
android:id="@+id/button2" android:text="Stop Service"
android:layout_width="match_parent"></Button>
</LinearLayout>


second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="@+id/textView1"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_height="wrap_content" android:text="Second
Activity" android:layout_width="match_parent"></TextView>
</LinearLayout>


Tut_2_4Activity.java
package com.tut2_4;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Tut_2_4Activity extends Activity implements
OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button start=(Button)findViewById(R.id.button1);
Button stop=(Button)findViewById(R.id.button2);
start.setOnClickListener(this);
stop.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v.getId()==R.id.button1)
{
startService(new Intent(this, MyService.class));
}
if(v.getId()==R.id.button2)
{
stopService(new Intent(this, MyService.class));
}
}
}

MyService.java
package com.tut2_4;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.SlidingDrawer;
import android.widget.Toast;
public class MyService extends Service{
@Override
public IBinder onBind(Intent intent) {
return null;
}
public void onCreate()
{
Toast.makeText(this, "My Service Started",
Toast.LENGTH_LONG).show();
}
public void onDestroy()
{
Toast.makeText(this, "My Service Stopped",
Toast.LENGTH_LONG).show();
}
public void onStart(Intent intent, int startid)
{
Intent dialogIntent = new Intent(getBaseContext(),
Second.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(dialogIntent);
}
}


Second.java
package com.tut2_4;
import android.app.Activity;
import android.os.Bundle;
public class Second extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
}
}


AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tut2_4"
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=".Tut_2_4Activity"
android:label="@string/app_name">
<intent-filter>
<action
android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="Second"></activity>
<service android:name="MyService"></service>
</application>
</manifest>


Output



No comments:

Post a Comment