2017-10-28 5 views
0

아래는 전자 메일을 보내기위한 java 및 xml 파일입니다. 코드를 확인하십시오. 버튼이 아직 작동하지 않습니다.암시 적 의도를 사용하여 전자 메일 보내기

ERROR: Failed to set EGL_SWAP_BEHAVIOR on surface 0xe2d19220, error=EGL_SUCCESS.Skipped 37 frames! The application may be doing too much work on its main thread

MainActivity.java

public void onClick(View view) {   
    Intent intent = null, 
    intent = new Intent(Intent.ACTION_SEND);  
    intent.setData(Uri.parse("mailto:")); 
    String[] to = {"[email protected]", ""}; 
    intent.putExtra(Intent.EXTRA_EMAIL, to); 
    intent.putExtra(Intent.EXTRA_SUBJECT, "subject to your app"); 
    intent.putExtra(Intent.EXTRA_TEXT, "text inside email"); 
    intent.setType("message/rfc822"); 
    chooser = Intent.createChooser(intent, "Send email"); 
    startActivity(chooser); 
} 

activity_main.xml 여기

<Button 
    android:id="@+id/btn2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Send Email" 
    /> 
+1

어떤 수단 "작동하지 않습니다" "자세히"설명해주십시오. ['ACTION_SENDTO]는 해당 추가 기능을 지원하는 것으로 문서화되어 있지 않습니다.] (https://developer.android.com/reference/android/content/Intent.html#ACTION_SENDTO), 'Uri'는 받는 사람을 식별하십시오. – CommonsWare

+0

버튼을 클릭해도 아무 일도 일어나지 않습니다. 여기서 내가 뭘 잘못하고 있니? – Honey

+0

아마도이 onClick() 메소드를 버튼에 연결하지 않았을 것입니다. 그것은 액티비티를 시작해야한다. 그렇지 않으면 당신은'ActivityNotFoundException'으로 충돌해야한다. – CommonsWare

답변

1

xml (btn1)에서와 code (btn2)에서 버튼을 연결할 수있는 방법입니다.

MainActivity.java

package stackoverflow.com.saturday; 

import android.content.Intent; 
import android.net.Uri; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 

public class MainActivity extends AppCompatActivity implements View.OnClickListener { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // thru code (btn2) 
     Button button = (Button)findViewById(R.id.btn2); 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Intent intent = new Intent(Intent.ACTION_SEND); 
       intent.setData(Uri.parse("mailto:")); 
       String[] to = {"[email protected]", ""}; 
       intent.putExtra(Intent.EXTRA_EMAIL, to); 
       intent.putExtra(Intent.EXTRA_SUBJECT, "subject to your app"); 
       intent.putExtra(Intent.EXTRA_TEXT, "text inside email"); 
       intent.setType("message/rfc822"); 
       Intent chooser = Intent.createChooser(intent, "Send email"); 
       startActivity(chooser); 
      } 
     }); 
    } 

    // thru xml (btn1) 
    @Override 
    public void onClick(View view) { 
     Intent intent = new Intent(Intent.ACTION_SEND); 
     intent.setData(Uri.parse("mailto:")); 
     String[] to = {"[email protected]", ""}; 
     intent.putExtra(Intent.EXTRA_EMAIL, to); 
     intent.putExtra(Intent.EXTRA_SUBJECT, "subject to your app"); 
     intent.putExtra(Intent.EXTRA_TEXT, "text inside email"); 
     intent.setType("message/rfc822"); 
     Intent chooser = Intent.createChooser(intent, "Send email"); 
     startActivity(chooser); 
    } 
} 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="stackoverflow.com.saturday.MainActivity"> 

    <Button 
     android:id="@+id/btn1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dp" 
     android:text="Send Email from xml" 
     android:onClick="onClick"/> 

    <Button 
     android:id="@+id/btn2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/btn1" 
     android:layout_marginTop="10dp" 
     android:text="Send Email from mainactivity"/> 

</RelativeLayout>