What Is an Intent?
Three types:activities, services, and broadcast receivers.
Intents can be passed to other applications written by other programmers, allowing them to be connected as modules of each other, if needed.
ACTION_DIAL Activity Displays thephone dialer
ACTION_BATTERY_LOW Broadcast ReceiverBattery low warning message
Implicit Intents & Explicit Intents
implicit intents is usually done via intent filters that are defined inthe AndroidManifest.xml file.
Intent filters are declared in AndroidManifest.xml using the <intent-filter> tag, and they filter based on three attributes of the Intent object; action,data, and category.
If no action filters are specified, the action parameter of the intent will not be tested at all,moving the testing on to the data parameter of the intent. If no data filters are specified, then only intents that contain no data will be matched.
specifies that video MPEG4 and audio MPEG3 can be retrieved from the internet via HTTP:
<intent-filter>
<data android:mimeType="video/mp4" android:scheme="http" />
<data android:mimeType="audio/mp3" android:scheme="http" />
</intent-filter>
(1)Using Intents with Activities
/layout/lock_analog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="You Are Currently in: Activity #1"
android:textColor="#fea"
android:textSize="22dip"
android:paddingBottom="20dip"/>
<Button
android:tag="btn1"
android:layout_width="200dip"
android:layout_height="40dip"
android:text="Go To Digital Clock: Activity #2"
android:textSize="10dip"
android:layout_gravity="center"/>
<AnalogClock
android:tag="analog1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dip"
android:background="@drawable/image1" />
</LinearLayout>
lock_digital.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="You Are Currently in: Activity #2"
android:textColor="#fea"
android:textSize="22dip"
android:paddingBottom="20dip"/>
<Button
android:tag="btn1"
android:layout_width="200dip"
android:layout_height="40dip"
android:text="Go To Analog Clock: Activity #1"
android:textSize="10dip"
android:layout_gravity="center"/>
<DigitalClock
android:tag="digital1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dip"
android:textSize="32dip"
android:textColor="#adf"
android:typeface="monospace"/>
</LinearLayout>
public class LockAnalogActivity extends Activity {
private View rootView;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
rootView=LayoutInflater.from(this).inflate(R.layout.lock_analog, null);
this.setContentView(rootView);
Button btn=(Button)rootView.findViewWithTag("btn1");
btn.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(v.getContext(),LockDigitalActivity.class);
startActivityForResult(intent,0);
}
});
}
}
public class LockDigitalActivity extends Activity {
private View rootView;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
rootView=LayoutInflater.from(this).inflate(R.layout.lock_digital, null);
this.setContentView(rootView);
Button btn=(Button)rootView.findViewWithTag("btn1");
btn.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Intent replyIntent=new Intent();
setResult(RESULT_OK,replyIntent);
finish();//send the intent back
}
});
}
}
(2)Using Intents with Services
A service needs to run asynchronously.A service can also be used by other Android applications, so it is more extensible than an activity.
You need to subclass the Service class and implement at least its onCreate(), onStart(), and onDestroy() methods with your own custom programming logic.
You also must declare the Service class in your AndroidManifest.xml file using the <service> tag.
/layout/lock_analog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="You Are Currently in: Activity #1"
android:textColor="#fea"
android:textSize="22dip"
android:paddingBottom="20dip"/>
<Button
android:tag="btn1"
android:layout_width="200dip"
android:layout_height="40dip"
android:text="Go To Digital Clock: Activity #2"
android:textSize="10dip"
android:layout_gravity="center"/>
<Button
android:tag="startBtn"
android:text="Start the Media Player Service"
android:textSize="10dip"
android:layout_width="200dip"
android:layout_height="40dip"
android:layout_gravity="center"/>
<Button
android:tag="stopBtn"
android:text="Stop the Media Player Service"
android:textSize="10dip"
android:layout_width="200dip"
android:layout_height="40dip"
android:layout_gravity="center"/>
<AnalogClock
android:tag="analog1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dip"
android:background="@drawable/image1" />
</LinearLayout>
AndroidManifest.xml
<service android:name=".MediaPlayerService" android:enabled="true"></service>
</application>
public class MediaPlayerService extends Service {
private MediaPlayer mPlayer;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
public void onCreate(){
mPlayer=MediaPlayer.create(this,R.raw.test);
mPlayer.setLooping(true);
}
public void onStart(Intent intent,int startId){
//trigger it with the start()
mPlayer.start();
}
public void onDestroy(){
//release memory containing the media player and the audio file
mPlayer.stop();
}
}
public class LockAnalogActivity extends Activity implements OnClickListener {
private View rootView;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
rootView=LayoutInflater.from(this).inflate(R.layout.lock_analog, null);
this.setContentView(rootView);
Button btn=(Button)rootView.findViewWithTag("btn1");
btn.setOnClickListener(this);
Button btnStart=(Button)rootView.findViewWithTag("startBtn");
btnStart.setOnClickListener(this);
Button btnStop=(Button)rootView.findViewWithTag("stopBtn");
btnStop.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
String tag=(String)v.getTag();
if("btn1".equals(tag)){
Intent intent=new Intent(v.getContext(),LockDigitalActivity.class);
startActivityForResult(intent,0);
}else if("startBtn".equals(tag)){
startService(new Intent(getBaseContext(),MediaPlayerService.class));
}else if("stopBtn".equals(tag)){
stopService(new Intent(v.getContext(),MediaPlayerService.class));
}
}
}
(3)Using Intents with Broadcast Receivers
lock_digital.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="You Are Currently in: Activity #2"
android:textColor="#fea"
android:textSize="22dip"
android:paddingBottom="20dip"/>
<Button
android:tag="btn1"
android:layout_width="200dip"
android:layout_height="40dip"
android:text="Go To Analog Clock: Activity #1"
android:textSize="10dip"
android:layout_gravity="center"/>
<DigitalClock
android:tag="digital1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dip"
android:textSize="32dip"
android:textColor="#adf"
android:typeface="monospace"/>
<EditText
android:tag="timeInSeconds"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:hint="Enter Number of Seconds Here!"
android:inputType="numberDecimal"
android:layout_marginTop="30dip"
android:layout_marginBottom="30dip" />
<Button
android:tag="startTimer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Start Timer Countdown" />
</LinearLayout>
AndroidManifest.xml
<receiver android:name=".TimerBroadcastReceiver" android:enabled="true" />
public class TimerBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, "Alarm Notification", Toast.LENGTH_LONG).show();
}
}
public class LockDigitalActivity extends Activity {
private View rootView;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
rootView=LayoutInflater.from(this).inflate(R.layout.lock_digital, null);
this.setContentView(rootView);
Button btn=(Button)rootView.findViewWithTag("btn1");
btn.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Intent replyIntent=new Intent();
setResult(RESULT_OK,replyIntent);
finish();//send the intent back
}
});
Button btnStartTimer=(Button)rootView.findViewWithTag("startTimer");
btnStartTimer.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
timerAlert(v);
}
});
}
private void timerAlert(View v){
EditText txtTime=(EditText)rootView.findViewWithTag("timeInSeconds");
int i=Integer.parseInt(txtTime.getText().toString());
Intent timerIntent=new Intent(this,TimerBroadcastReceiver.class);
PendingIntent myPindingIntent=PendingIntent.getBroadcast(this.getApplicationContext(), 0, timerIntent, 0);
AlarmManager myAlarmManager=(AlarmManager)getSystemService(ALARM_SERVICE);
myAlarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+(i*1000), myPindingIntent);
Toast.makeText(this, "Alarm is set for "+i+" seconds", Toast.LENGTH_LONG).show();
}
}