Beruflich Dokumente
Kultur Dokumente
30
C Wouldn't it be cool
Right now
the temperature in Mumbai is
KNOW MORE
activity_main.xml
In this example, we don't have any component in this file..
Activity class
Let's write the code to know the call state.
File: MainActivity.java
1.
packagecom.javatpoint.callstates;
2.
3.
importandroid.os.Bundle;
4.
importandroid.app.Activity;
5.
importandroid.content.Context;
6.
importandroid.telephony.PhoneStateListener;
7.
importandroid.telephony.TelephonyManager;
8.
importandroid.view.Menu;
9.
importandroid.widget.Toast;
10.
11.
publicclassMainActivityextendsActivity{
12.
13.
@Override
14.
protectedvoidonCreate(BundlesavedInstanceState){
15.
super.onCreate(savedInstanceState);
16.
setContentView(R.layout.activity_main);
17.
18.
TelephonyManagertelephonyManager=
19.
(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
20.
21.
PhoneStateListenercallStateListener=newPhoneStateListener(){
22.
publicvoidonCallStateChanged(intstate,StringincomingNumber)
23.
24.
if(state==TelephonyManager.CALL_STATE_RINGING){
25.
Toast.makeText(getApplicationContext(),"PhoneIsRiging",
26.
Toast.LENGTH_LONG).show();
27.
28.
if(state==TelephonyManager.CALL_STATE_OFFHOOK){
29.
Toast.makeText(getApplicationContext(),"PhoneisCurrentlyinAcall",
30.
Toast.LENGTH_LONG).show();
Right now
the
temperature in
Mumbai is
30
Wouldn't it
be cool
if your fan
could adjust
to it?
KNOW MORE
31.
32.
33.
if(state==TelephonyManager.CALL_STATE_IDLE){
34.
Toast.makeText(getApplicationContext(),"phoneisneitherringingnorinacall",
35.
Toast.LENGTH_LONG).show();
36.
37.
38.
};
39.
telephonyManager.listen(callStateListener,PhoneStateListener.LISTEN_CALL_STATE);
40.
41.
42.
43.
@Override
44.
publicbooleanonCreateOptionsMenu(Menumenu){
45.
//Inflatethemenu;thisaddsitemstotheactionbarifitispresent.
46.
getMenuInflater().inflate(R.menu.main,menu);
47.
returntrue;
48.
49.
50.
AndroidManifest.xml
You need to provide READ_PHONE_STATE permission in the AndroidManifest.xml file.
File: AndroidManifest.xml
1.
<?xmlversion="1.0"encoding="utf-8"?>
2.
<manifestxmlns:androclass="http://schemas.android.com/apk/res/android"
3.
package="com.javatpoint.callstates"
4.
android:versionCode="1"
5.
android:versionName="1.0">
6.
7.
<uses-sdk
8.
android:minSdkVersion="8"
9.
android:targetSdkVersion="17"/>
10.
11.
12.
<uses-permissionandroid:name="android.permission.READ_PHONE_STATE"/>
13.
14.
<application
15.
android:allowBackup="true"
16.
android:icon="@drawable/ic_launcher"
17.
android:label="@string/app_name"
18.
android:theme="@style/AppTheme">
19.
<activity
20.
android:name="com.javatpoint.callstates.MainActivity"
21.
android:label="@string/app_name">
22.
<intent-filter>
23.
<actionandroid:name="android.intent.action.MAIN"/>
24.
25.
<categoryandroid:name="android.intent.category.LAUNCHER"/>
26.
</intent-filter>
27.
</activity>
28.
</application>
29.
30.
</manifest>
<<prev
next>>