简体   繁体   中英

How to trigger a class extending BraodcastReceiver

I am developing a app that changes the Ringer mode on some specific Numbers .

I have a class extending BroadcastReceiver to get the incoming number . the of this class is this ::

    package com.example.callchecker;

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



    public class Main_Activity 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");

//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("MY_DEBUG_TAG",state);
            if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
                String phonenumber = extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);

                // AM_str=String.valueOf(ringer_mode);
                //AM.setRingerMode(1);
                ringer_mode =AM.getRingerMode();

                // AM_str=String.valueOf(ringer_mode);

            //  Log.w("Ringer_mode at ringing", AM_str);

            //  Log.w("MY_DEBUG_TAG", phonenumber);

                if (phonenumber.equals("0046705888806")){
                //  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);

    }

    }

    }

And the Activity that is set for launch is this :

    package com.example.callchecker;

    import android.app.Activity;
    //import android.app.Dialog;
    import android.content.Context;
    import android.os.Bundle;
    import android.util.Log;
    //import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    //import android.content.BroadcastReceiver;
    //import android.content.Intent;;

    public class Mark_number extends Activity {
Context context = this ;

Button btn_dialog = (Button) findViewById(R.id.btn_dialog);

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mark_number);

            Log.w("start", "start");


    btn_dialog.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Log.w("Mark_number", "click");
        }
    });

    }

And the AndroidManifist.xml is this :

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

    <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" >

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

    </uses-permission>


    <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name="com.example.callchecker.Main_Activity"
        android:label="@string/app_name" >

    </activity>

        <activity
        android:name="com.example.callchecker.Mark_number"
        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="Main_Activity" >
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE"/>


        </intent-filter>

    </receiver>
        </application>

    </manifest>

Q 1 ). I want to get the phone number weather my application is not running . Is this the right way to do this ???

Q 2 ). Weather i have to call the Main_Activity for register the Receiver ????

将所有号码存储在所需的共享首选项或数据库中。如果呼叫来自特定号码,则更改电话状态。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM