Vahid
Vahid

Reputation: 1378

How to pass a variable to broadCastReceiver

I'm working on my final project ,please help me ,i have to submit it till end of next week im working on an app that filter incoming calls based of context, at first i want to take incoming number and pass to an Activity to search number on dataBase and return the result ,i dont know how to get variable from a broadcastReceiver and pass to it again ,i mean get and pass variable to just 1 broadcastReceiver so i decided to use 2 BroadCastReceiver ,on of them take the incoming number and pass to my mainclass and my mainCalss search incoming number on database and pass the result to filterCall broadcast class,but i faced with problem .please help me,tHX

This is my callActivity Broadcast class that take incoming number and pass to my Mainclass

    public class CallblockingActivity extends  BroadcastReceiver {


          Context context = null;
          private static final String TAG = "Phone call";
          private ITelephony telephonyService;
          String incommingNumber;
           Intent intent;
           String n;

        @Override

          public void onReceive(Context context, Intent intent) {
                   Log.v(TAG, "Receving....");
                   TelephonyManager telephony = (TelephonyManager) 
                   context.getSystemService(Context.TELEPHONY_SERVICE);  
                     try {
                         Class c = Class.forName(telephony.getClass().getName());
                         Method m = c.getDeclaredMethod("getITelephony");
                         m.setAccessible(true);
                         telephonyService = (ITelephony) m.invoke(telephony);           
                         Bundle b = intent.getExtras();
                         incommingNumber = b.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
                         Log.v(TAG, incommingNumber);
                         Intent i = new Intent(context, Mainclass.class);
                         i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                         i.putExtra("n",incommingNumber);
                         context.startActivity(i);
                         Log.v(TAG, incommingNumber);

                     }
                     catch (Exception e) {
                      e.printStackTrace();
                     }

            }

  }   

And this is my mainClass that take incoming number from CallBlockingActivity broadcast class and search on database and return a boolean value

 public class Mainclass  extends Activity{
Context context = null;
Boolean isit= false;
String TAG;

   public void onCreate(Bundle savedInstanceState) {
        Log.v(TAG,"onCreate");
        super.onCreate(savedInstanceState);
       setContentView(R.layout.main);


       Intent b= getIntent();
        String incomiingnumber1= b.getStringExtra("n");
        Log.v(TAG,incomiingnumber1 + "incomenuber from broadcast receiver");
        DataBaseBON searching= new DataBaseBON(Mainclass.this);
       searching.open();
        isit= searching.searchnumber(incomiingnumber1);
        String ss= new Boolean(isit).toString();       
       Toast.makeText(getApplicationContext(), ss, Toast.LENGTH_SHORT);
       Log.v(TAG, ss+ "yeeeeeeees it is it");
       searching.close();

      //  I think My problem is here
       Intent in = new Intent(context, FilterCall.class);
       Bundle bb = new Bundle();
       bb.putString("keyvalue", "ss");
       in.putExtras(bb);
       startActivity(in);

   }

}

And this is my Filter BroadcastClass that reject the call or set the phone on silent mode.

public class FilterCall  extends BroadcastReceiver {

private ITelephony telephonyService;
String TAG;
@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub


    Log.v(TAG, "Filtering...");
    TelephonyManager telephony = (TelephonyManager) 
    context.getSystemService(Context.TELEPHONY_SERVICE);  
      try {
         Class c = Class.forName(telephony.getClass().getName());
         Method m = c.getDeclaredMethod("getITelephony");
          m.setAccessible(true);
          telephonyService = (ITelephony) m.invoke(telephony);          

          String result = intent.getStringExtra("keyvalue");


                if (result=="true") {
                try {
                    telephonyService.silenceRinger();
                    telephonyService.endCall();
                } catch (RemoteException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


           }


      }
      catch (Exception e) {
       e.printStackTrace();
      }

 }

}

I am faced with this problem:

    08-05 16:58:50.468: W/IInputConnectionWrapper(1500): showStatusIcon on inactive   InputConnection
    08-05 16:58:50.958: V/(1500): Filtering...
  08-05 16:58:51.118: V/Phone call(1500): Receving....
 08-05 16:58:51.118: V/Phone call(1500): +60172214866
 08-05 16:58:51.138: V/Phone call(1500): +60172214866
   08-05 16:58:51.168: V/(1500): onCreate
 08-05 16:58:51.248: V/(1500): +60172214866incomenuber from broadcast receiver
 08-05 16:58:51.278: I/Database(1500): SQLiteDatabase open path:  /data/data/com.vahid.davoudi/databases/databasebon.db
 08-05 16:58:51.338: V/(1500): trueyeeeeeeees it is it
   08-05 16:58:51.338: I/Database(1500): SQLiteDatabase close path:  /data/data/com.vahid.davoudi/databases/databasebon.db
 08-05 16:58:51.348: A/dalvikvm(1500): Exception!!! threadid=1: thread exiting with         uncaught exception (group=0x4001d858)
 08-05 16:58:51.388: E/AndroidRuntime(1500): FATAL EXCEPTION: main
 08-05 16:58:51.388: E/AndroidRuntime(1500): java.lang.RuntimeException: Unable to start  activity ComponentInfo{com.vahid.davoudi/com.vahid.davoudi.Mainclass}:  java.lang.NullPointerException
   08-05 16:58:51.388: E/AndroidRuntime(1500):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
 08-05 16:58:51.388: E/AndroidRuntime(1500):    at  android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
  08-05 16:58:51.388: E/AndroidRuntime(1500):   at android.app.ActivityThread.access$2300(ActivityThread.java:125)
 08-05 16:58:51.388: E/AndroidRuntime(1500):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
  08-05 16:58:51.388: E/AndroidRuntime(1500):   at android.os.Handler.dispatchMessage(Handler.java:99)
   08-05 16:58:51.388: E/AndroidRuntime(1500):  at android.os.Looper.loop(Looper.java:123)
  08-05 16:58:51.388: E/AndroidRuntime(1500):   at android.app.ActivityThread.main(ActivityThread.java:4627)
  08-05 16:58:51.388: E/AndroidRuntime(1500):   at java.lang.reflect.Method.invokeNative(Native Method)
  08-05 16:58:51.388: E/AndroidRuntime(1500):   at java.lang.reflect.Method.invoke(Method.java:521)
  08-05 16:58:51.388: E/AndroidRuntime(1500):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
  08-05 16:58:51.388: E/AndroidRuntime(1500):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
  08-05 16:58:51.388: E/AndroidRuntime(1500):   at dalvik.system.NativeStart.main(Native Method)
  08-05 16:58:51.388: E/AndroidRuntime(1500): Caused by: java.lang.NullPointerException
  08-05 16:58:51.388: E/AndroidRuntime(1500):   at android.content.ComponentName.<init>(ComponentName.java:75)
      08-05 16:58:51.388: E/AndroidRuntime(1500):   at android.content.Intent.<init>(Intent.java:2690)
  08-05 16:58:51.388: E/AndroidRuntime(1500):   at com.vahid.davoudi.Mainclass.onCreate(Mainclass.java:39)
 08-05 16:58:51.388: E/AndroidRuntime(1500):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
  08-05 16:58:51.388: E/AndroidRuntime(1500):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
   08-05 16:58:51.388: E/AndroidRuntime(1500):  ... 11 more
  08-05 17:03:51.408: W/PrintK(1500): m.vahid.davoudi(1500) sends SIGKILL to        m.vahid.davoudi(1500)

And this is my manifest

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

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

 <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".Mainclass"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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


    <activity
        android:name=".CallBlockingOrginalActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".ViewDatabase"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.VIEWDB" />

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

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

    <receiver  android:name=".FilterCall">
        <intent-filter  android:priority="100" >
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>
  </application>

 </manifest>

Upvotes: 2

Views: 2589

Answers (2)

AAnkit
AAnkit

Reputation: 27549

Some suggestion for your problem,

1) you dont need to add that much code in your Broadcast receiver to get incoming number. while you can directly use below code to get this.

String number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);

2) You don't need to start a activity to search the database, You can directly search this number in database from Receivers on receive method(hope your database is not too big which can take more time to cause ANR)

3) you can directly do what you are doing in Another receiver in first receiver only. it will in terms removes lots of code and make it a good approach.

for local issue in your code, you are using below code to send a broadcast message

//  I think My problem is here
       Intent in = new Intent(context, FilterCall.class);
       Bundle bb = new Bundle();
       bb.putString("keyvalue", "ss");
       in.putExtras(bb);
       startActivity(in);

but you are calling startActivity instead of sendBroadcast method cause FilterCall is a Broadcast Receiver.

you should do something like this

       Intent in = new Intent("Intent Action which your receiver is listening to");
       in.putExtras("keyvalue", "ss");
      sendBroadcast(in);

Upvotes: 1

BrainCrash
BrainCrash

Reputation: 13172

You have a nullPointerException at line 39 of Mainclass.java

It is possible that "b" returns null

Intent b = getIntent();

add

if(b != null) {
    //code involving the use of b
}

Upvotes: 1

Related Questions