0

Hello! I have an application which reads call logs, incoming sms, and outgoing sms.

When the application is first launched, it shows call logs and then listens for incoming and outgoing sms. I have three classes: Main Activity, IncomingSms.Java and outgoingSms.java.

The main activity starts normally and shows everything fine, but the incoming and outgoing sms functions are not triggered. My incoming and outgoing sms classes are 100% correct and works fine if I create and run as new project but they don't work together.

All I want is to trigger all these processes simultaneously here is my code.

MainActivity.java

public class MainActivity extends Activity {

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

    @SuppressWarnings("deprecation")
    private void getCallDetails() {

        //rest of code here


    }

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

my IncmingSms.java

public class IncomingSms extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        final Bundle bundle = intent.getExtras();
        final SmsManager sms = SmsManager.getDefault();
        // REST OF CODE HERE
    }
}

OutgoingSms.java

public class OutgoingSms extends Activity {
    final SmsManager sms = SmsManager.getDefault();

    ContentResolver contentResolver;
    ContentObserver smsContentObserver;

    @Override
    public void onResume() {
        super.onResume();
        smsContentObserver = new ContentObserver(new Handler()) {

            @Override
            public void onChange(boolean selfChange) {
                Uri smsURI = Uri.parse("content://sms/sent");
                Cursor c = getContentResolver().query(smsURI, new String[] {
                    "address", "date", "body", "type"
                }, null, null, null);
                String[] columns = new String[] {
                    "address", "date", "body", "type"
                };
                c.moveToNext();
                Rest of Code here@ Override
                public boolean deliverSelfNotifications() {
                    return true;
                }
            };

            contentResolver.registerContentObserver(Uri.parse("content://sms"), true, smsContentObserver);

        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            contentResolver.unregisterContentObserver(smsContentObserver);
        }
    }
Steven Huang
  • 490
  • 4
  • 16
Rida Shahid
  • 386
  • 7
  • 22

1 Answers1

0

You can merge OutgoingSms.java and MainActivity.java.Also you can notify your MainActivity from IncomingSms when it receives new sms.You can see more details here:

how can I notify a running activity from a broadcast receiver?

How can I send result data from Broadcast Receiver to Activity

Android BroadcastReceiver Tutorial

Community
  • 1
  • 1
hasanghaforian
  • 13,858
  • 11
  • 76
  • 167