AlarmServiceActivity.java
package com.ann;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class AlarmServiceActivity extends Activity {
EditText txt;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void start(View v){
txt=(EditText)findViewById(R.id.txt);
int i=Integer.parseInt(txt.getText().toString());
Intent intent=new Intent(this,myBroadcast.class);
PendingIntent intt=PendingIntent.getBroadcast(this, 234, intent, 0);
AlarmManager alarm=(AlarmManager)getSystemService(ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+(i*1000), intt);
//startService(new Intent(this,Myservice.class));
Toast.makeText(this, "Alarm set in " + i + " seconds",
Toast.LENGTH_LONG).show();
}
public void stop(View v){
Intent i=new Intent(this,Myservice.class);
stopService(i);
}
}
myBroadcast.java
package com.ann;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.sax.StartElementListener;
import android.widget.Toast;
public class myBroadcast extends BroadcastReceiver {
//MediaPlayer mp;
@Override
public void onReceive(Context context, Intent intent) {
Intent sintent=new Intent(context,AlarmServiceActivity.class);
sintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(sintent);
Toast.makeText(context, "Don't panik but your time is up!!!!.",
Toast.LENGTH_LONG).show();
context.startService(new Intent(context,Myservice.class));
}
}
Myservice.java
package com.ann;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
public class Myservice extends Service {
MediaPlayer mp;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
public void onCreate(){
mp=new MediaPlayer();
mp=MediaPlayer.create(this, R.raw.roja);
//mp.setLooping(false);
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
try{
Toast.makeText(this, "My service started ",Toast.LENGTH_LONG).show();
Log.w("inside","onStart");
mp.start();
}catch(Exception e){}
}
@Override
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d("inside", "onDestroy");
mp.stop();
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ann"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".AlarmServiceActivity"
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="Myservice"></service>
<receiver android:name="myBroadcast"></receiver>
</application>
</manifest>
No comments:
Post a Comment