您好!我有一个应用程序,它读取呼叫日志,传入短信和传出短信。
首次启动应用程序时,它会显示呼叫日志,然后侦听传入和传出的短信。我有三个类:Main Activity,IncomingSms.Java和outgoingSms.java。
主要活动正常启动并显示一切正常,但不会触发传入和传出的短信功能。我的传入和传出短信类是100%正确的,如果我创建和运行新项目但它们不能一起工作,它们工作正常。
我想要的只是同时触发所有这些进程,这是我的代码。
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;
}
}
我的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);
}
}
答案 0 :(得分:0)
您可以合并OutgoingSms.java
和MainActivity.java
。此外,您可以在MainActivity
收到新的短信时通知您IncomingSms
。您可以在此处查看更多详情:
how can I notify a running activity from a broadcast receiver?
How can I send result data from Broadcast Receiver to Activity