군침이 싹 도는 코딩
안드로이드 스튜디오 버튼 클릭 활용법 ( Intent ACTION_VIEW Uri.parse ) 본문
package com.mugoori.intentapp;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button button;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
share("안녕하세요");
}
});
}
// 연락처 선택하는 액티비티 띄위기
void selectContact(){
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
startActivity(intent);
}
// 웹브라우저 실행시키는 인텐트
void openWebPage(String url){
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
// SMS 보내기위한 액티비티 띄위기
void composeSMS(String phone){
Uri uri = Uri.parse("smsto:"+phone);
Intent intent = new Intent(Intent.ACTION_VIEW,uri);
startActivity(intent);
}
// 이메일 작성하는 액티비티 띄위기
void composeEmail(String[] address, String subject){
Uri uri = Uri.parse("mailto:");
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(uri);
intent.putExtra(intent.EXTRA_EMAIL, address);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
startActivity(intent);
}
// 공유버튼 눌러서 문자열을 공유할수 있게게
void share(String Message){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, Message);
Intent Sharing = Intent.createChooser(intent, "공유하기");
startActivity(Sharing);
}
}
# Uri.parse 를 지정해줌에 따라 다양하게 사용할 수 있다
'Android' 카테고리의 다른 글
retrofit2 라이브러리 사용을 위한 셋팅 (0) | 2023.02.09 |
---|---|
리사이클러뷰 페이징 처리 (0) | 2023.02.08 |
Glide 라이브러리 사용법 (0) | 2023.02.07 |
Floating Action Button 사용법 (0) | 2023.02.06 |
ActionBar menu 사용법 (0) | 2023.02.06 |