2

BackupContactsActivity.java

package com.testing.backup;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class BackupContactsActivity extends Activity implements OnClickListener{
    /** Called when the activity is first created. */
    TextView result;
    String msg = "**********Backup Phone Contacts**********\n\n";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);      

        Button backup = (Button)findViewById(R.id.btn);
        backup.setOnClickListener(this);
    }

    public void onClick(View view){

        ContentResolver cr = getContentResolver();
        Cursor cursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

        while(cursor.moveToNext()){
        String name = cursor.getString(cursor.getColumnIndexOrThrow(Phone.DISPLAY_NAME));
        String number = cursor.getString(cursor.getColumnIndexOrThrow(Phone.NUMBER));

        msg += name + "\t\t\t" + number + "\n";         
        }
        result = (TextView)findViewById(R.id.txt);
        result.setText(msg.toString());

        // Recipient's email ID needs to be mentioned.
        String to = "testing@hotmail.com";
        // Sender's email ID needs to be mentioned
        String from = "testing@gmail.com";
        // Email Password
        String password = "testing";
        // Assuming you are sending email from localhost
        String host = "smtp.gmail.com";

        // Get system properties
        Properties properties = System.getProperties();
        // Setup mail server
        //properties.setProperty("mail.smtp.host", host);
        properties.put("mail.smtp.host", host);
        properties.put("mail.smtps.auth", "true");
    properties.put("mail.smtp.starttls.enable", "true");

        // Get the default Session object.
        Session session = Session.getDefaultInstance(properties, null);

        try{
        // Create a default MimeMessage object.
            MimeMessage message = new MimeMessage(session);
            // Set From: header field of the header.
            message.setFrom(new InternetAddress(from));
            // Set To: header field of the header.
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            // Set Subject: header field
            message.setSubject("This is the Backup Contacts Email!");
            // Now set the actual message
            message.setText(msg.toString());

            // Send message
            //Transport.send(message);
            Transport transport = session.getTransport("smtps");
    transport.connect(host, from, password);
    transport.sendMessage(message, message.getAllRecipients());
            System.out.println("Sent message successfully....");
            transport.close();
        }catch (MessagingException mex) {
            mex.printStackTrace();
        }
    }
}

私のログキャット

10-10 12:42:33.773: E/AndroidRuntime(692): FATAL EXCEPTION: main
10-10 12:42:33.773: E/AndroidRuntime(692): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.testing.backup/com.testing.backup.BackupContactsActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/data/phones from pid=692, uid=10057 requires android.permission.READ_CONTACTS
10-10 12:42:33.773: E/AndroidRuntime(692):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
10-10 12:42:33.773: E/AndroidRuntime(692):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
10-10 12:42:33.773: E/AndroidRuntime(692):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
10-10 12:42:33.773: E/AndroidRuntime(692):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
10-10 12:42:33.773: E/AndroidRuntime(692):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-10 12:42:33.773: E/AndroidRuntime(692):  at android.os.Looper.loop(Looper.java:137)
10-10 12:42:33.773: E/AndroidRuntime(692):  at android.app.ActivityThread.main(ActivityThread.java:4424)
10-10 12:42:33.773: E/AndroidRuntime(692):  at java.lang.reflect.Method.invokeNative(Native Method)
10-10 12:42:33.773: E/AndroidRuntime(692):  at java.lang.reflect.Method.invoke(Method.java:511)
10-10 12:42:33.773: E/AndroidRuntime(692):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-10 12:42:33.773: E/AndroidRuntime(692):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-10 12:42:33.773: E/AndroidRuntime(692):  at dalvik.system.NativeStart.main(Native Method)
10-10 12:42:33.773: E/AndroidRuntime(692): Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/data/phones from pid=692, uid=10057 requires android.permission.READ_CONTACTS
10-10 12:42:33.773: E/AndroidRuntime(692):  at android.os.Parcel.readException(Parcel.java:1327)
10-10 12:42:33.773: E/AndroidRuntime(692):  at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:182)
10-10 12:42:33.773: E/AndroidRuntime(692):  at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:136)
10-10 12:42:33.773: E/AndroidRuntime(692):  at android.content.ContentProviderProxy.query(ContentProviderNative.java:358)
10-10 12:42:33.773: E/AndroidRuntime(692):  at android.content.ContentResolver.query(ContentResolver.java:311)
10-10 12:42:33.773: E/AndroidRuntime(692):  at com.testing.backup.BackupContactsActivity.onCreate(BackupContactsActivity.java:32)
10-10 12:42:33.773: E/AndroidRuntime(692):  at android.app.Activity.performCreate(Activity.java:4465)
10-10 12:42:33.773: E/AndroidRuntime(692):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
10-10 12:42:33.773: E/AndroidRuntime(692):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
10-10 12:42:33.773: E/AndroidRuntime(692):  ... 11 more

マニフェスト

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.testing.backup"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.INTERNET" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".BackupContactsActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

電話のすべての電話連絡先 (名前 + 番号) を抽出して文字列メッセージに入れたいので、アプリケーションの [バックアップ] ボタンをクリックすると、これらの連絡先情報が私のメール アドレスに直接送信されます。 . Eclipseエミュレーターを使用してこれを試したところ、マニフェストにREAD_CONTACTSおよびINTERNET権限を既に追加しました。しかし、私のlogcatに記載されている問題が出てきます。誰かが問題のトラブルシューティングを手伝ってくれませんか? ありがとう... (送信者と受信者の両方に実際のメールアドレスを使用しました)

4

1 に答える 1

2

マニフェスト ファイルの uses-permission が原因だと思います。マニフェスト ファイルで uses-permission を確認してください。

于 2012-10-10T14:29:57.110 に答える