2016-02-24 45 views
0

我有一个应用程序,我应该读取一个调用,然后自动发送一条消息给调用者。我以编程方式创建了消息,但我决定让用户在数据库中为自己创建消息。现在我将这条消息保存在数据库中,并可以使用烤面包片实际查看它。但问题是现在没有将消息作为消息发送。我认为问题是在通话期间从数据库加载数据。因此,我想问一下,如果有人知道一个线程我可以使用,使这项工作异步请帮我...Android SQLite和线程

下面是相关代码

NotificationsDatabaseAdapter simsHelper; 

case TelephonyManager.CALL_STATE_RINGING: 
     final String S1 = "kingmaker"; 
     String notificationText = simsHelper.getData(S1); 

     try { 


      SmsManager smsManager = SmsManager.getDefault(); 
      smsManager.sendTextMessage(toPhoneNumber, null, notificationText, 
        null, null); 
      Toast.makeText(getApplicationContext(), "SMS sent.", 
        Toast.LENGTH_LONG).show(); 
     } catch (Exception e) { 
      Toast.makeText(getApplicationContext(), "Sending SMS failed.", 
        Toast.LENGTH_LONG).show(); 
      e.printStackTrace(); 
     } 

回答

0

我找到了解决方案通过加载数据库时我的TeleListener在主要活动中启动。不需要线程,因为在发出呼叫时所有的数据都已经被加载。

private void initializeCallListener(){ TelephonyManager TelephonyMgr =(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); TeleListener监听器= new TeleListener(); listener.setApplicationContext(getApplicationContext()); TelephonyMgr.listen(listener,PhoneStateListener.LISTEN_CALL_STATE); savedData(); //加载我的数据库 }