1

Hello guys i am trying to send two variables from an android service to a broadcast receiver, and i need help here..

i am setting up two var's in the oncreate method of the service class here..

    @Override
public void onCreate() {

    super.onCreate();


    Intent eSendIntent = new Intent(getApplicationContext(), OutgoingCallReceiver.class);

    eSendIntent.putStringArrayListExtra("BlockArray", contactsListB);
    eSendIntent.putExtra("BlockBool", checkB);
    getApplicationContext().sendOrderedBroadcast(eSendIntent, null);//Call receiver



 }

and in my receiver class...

onReceive(Context context, Intent intent){
Bundle bundle = intent.getExtras();

            if(bundle == null)
                    return;


            boolean cb = bundle.getBooleanExtra("BlockBool", true);
            ArrayList<String> ab = bundle.getStringArrayListExtra("BlockArray");


//disconnecting
            try{

                if(cb==false){
                for(int ij = 0; ij < ab.size(); ij++){
                    if(ab.get(ij).contains(phonenumber)){

                        tempBoolean = true;
                        //Log.e("OutgoingCallReceiver", SmsBlockerService.contactsListB.get(ij));
                    }
                }//for loop
                    if(tempBoolean==true){

                setResultData(null);
                        Toast.makeText(context, phonenumber + " is Blocked", Toast.LENGTH_SHORT).show();
                    }

            }else{
                setResultData(null);

                Toast.makeText(context, "All Out-Going Calls are Blocked", Toast.LENGTH_SHORT).show();
            }//end of main if

                    } catch(Exception e){
                        Toast.makeText(context, "Detect Calls sample application Failed: ", Toast.LENGTH_LONG).show();
             }
}

logcat:

E/BroadcastReceiver(1459): BroadcastReceiver trying to return result during a non-ordered broadcast

4

3 回答 3

1

在您的广播意图中设置它

         i.setAction("MYACTION");

而不是在你的清单中设置这个

   <receiver android:name=".BroadcastClass" >
        <intent-filter>
            <action android:name="MYACTION" />
        </intent-filter>
    </receiver>

可能这应该有帮助

于 2013-05-30T09:08:44.943 回答
0

Assuming that you registered the receiver.Now try to make a small change:

        boolean cb = bundle.getBooleanExtra("BlockBool", true);
        ArrayList<String> ab = bundle.getStringArrayListExtra("BlockArray");

Update:

In the on create... call the broadcast like...

  sendOrderedBroadcast(eSendIntent);

You are facing that error because , setResultData() function works only with OrderedBroadcast.

From Android Documentation:

   public final void setResultCode (int code)

   Added in API level 1
   Change the current result code of this broadcast; only works with broadcasts sent
   through Context.sendOrderedBroadcast. Often uses the Activity RESULT_CANCELED and  
   RESULT_OK constants, though the actual meaning of this value is ultimately up to the
   broadcaster.

   This method does not work with non-ordered broadcasts such as those sent with 
   Context.sendBroadcast.
于 2013-05-30T08:58:35.150 回答
0

为了让广播接收器工作,写入它的活动/屏幕应该启动并运行。以便接收者可以接收传递的意图及其值。

于 2013-05-30T09:02:24.030 回答