0

如果呼叫来自特定号码,我想制作一个获取来电号码的应用程序,然后做一些事情。即使应用程序没有运行,我也想获取来电号码。我正在使用 BraodcastReceiver 来获取来电号码。

我有两个 java 类,一个扩展他的活动,另一个扩展 BraodcastReceiver 以获取来电号码。

扩展活动的主类:

package digicare.ringmanager;

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

public class Main_Activity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main_layout, menu);
        return true;
    }
}

它非常简单,并且 Number checker 类扩展了 BroadcastReceiver :

package digicare.ringmanager;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;

public class number_checker extends BroadcastReceiver {
    private int ringer_mode ;
    private String AM_str;
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        AudioManager AM =(AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
        Log.w("start", "starting the Main_Activity");

        AM_str=String.valueOf(ringer_mode);
        Log.w("Ringer_mode at start", AM_str);

        //setting the ringer mode on number match
        try {
            Bundle extras=intent.getExtras();
            if (extras !=null){

                String state = extras.getString(TelephonyManager.EXTRA_STATE);
                Log.w("state at start",state);
                if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
                    String phonenumber = extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);


                    //AM.setRingerMode(1);
                    ringer_mode =AM.getRingerMode();
                    AM_str=String.valueOf(ringer_mode);
                    Log.w("Ringer_mode at ringing", AM_str);

                    Log.w("Number", phonenumber);

                    if (phonenumber.equals("1234")){
                        Log.w("yahoo", "Number matched");

                        if (ringer_mode==AudioManager.RINGER_MODE_SILENT || ringer_mode==AudioManager.RINGER_MODE_VIBRATE ){

                            AM.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                            Log.w("Phone number is matched .!", "Now , ringer mode is normal");
                            int now_nor =AM.getRingerMode();
                            String now_nor_str=String.valueOf(now_nor);
                            Log.w("ring_mode at num matched",now_nor_str);

                        }
                    }

                }
                if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK ) || state.equals(TelephonyManager.EXTRA_STATE_IDLE)){

                    int now_nor =AM.getRingerMode();
                    String now_nor_str=String.valueOf(now_nor);
                    Log.w("ring_mode at offHock",now_nor_str);

                    if (ringer_mode==AudioManager.RINGER_MODE_NORMAL){
                        AM.setRingerMode(AudioManager.RINGER_MODE_NORMAL );
                        Log.w("Normal", "");
                    }else if (ringer_mode==AudioManager.RINGER_MODE_SILENT ){
                        AM.setRingerMode(AudioManager.RINGER_MODE_SILENT );
                        Log.w("silent", "");
                    }else if (ringer_mode==AudioManager.RINGER_MODE_VIBRATE ){
                        AM.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
                        Log.w("vibrat", "");
                    }
                    // Log.w("Again", "Now the Ringer mode is get back ");

                    int now =AM.getRingerMode();
                    String now_str=String.valueOf(now);
                    Log.w("ring_mode at end ",now_str);
                }   
            }
        } catch (Exception e) {
            // TODO: handle exception
            Log.w("MY_DEBUG_TAG", e);

        }
    }
}

AndroidManifist.xml 是这样的:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="digicare.ringmanager"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
    android:minSdkVersion="5"
    android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>


    <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="digicare.ringmanager.Main_Activity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

       <receiver android:name="number_checker" >
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE"/>


        </intent-filter>

    </receiver>

    </application>

    </manifest>

但这不起作用,无法获取来电号码。我能做什么????必须调用 number_checker 类来注册 Braodcast ???

请帮助我是一个新的安卓开发者

4

1 回答 1

0

您可以动态注册此类的实例,也可以Context.registerReceiver()通过AndroidManifest.xml.

注意:如果在你的Activity.onResume()实现中注册一个接收器,你应该在Activity.onPause(). (暂停时您不会收到意图,这将减少不必要的系统开销)。不要在 中取消注册Activity.onSaveInstanceState(),因为如果用户移回历史堆栈,则不会调用它。

还要检查这个链接:

http://www.vogella.com/articles/AndroidBroadcastReceiver/article.html

新更新

检查此链接http://androidexample.com/Incomming_Phone_Call_Broadcast_Receiver__-_Android_Example/index.php?view=article_discription&aid=61&aaid=86

更新 2

放在.收件人姓名之前。像这样<receiver android:name=".number_checker" >

于 2013-07-07T06:31:55.607 回答