Android

FCM 푸시 알림

    manifest 에 서비스 등록 <service android:name=”.MyFireBaseMessagingService”> <intent-filter> <action android:name=”com.google.firebase.MESSAGING_EVENT” /> </intent-filter> </service>   인터넷 권한 허용 <uses–permission android:name=“android.permission.INTERNET”></uses–permission>     MyFireBaseMessagingService.java   public class MyFireBaseMessagingService extends FirebaseMessagingService { @Override public void onNewToken(String token) { Log.d(“FCM Log”, “Refreshed token: ” + token); } @Override public void onMessageReceived(RemoteMessage remoteMessage) { if (remoteMessage.getNotification() != null) { Log.d(“FCM Log”, “알림 메시지: ” + remoteMessage.getNotification().getBody()); String messageBody = remoteMessage.getNotification().getBody(); String messageTitle = remoteMessage.getNotification().getTitle(); Intent intent = new Intent(this, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT); String channelId = “Channel ID”; Uri…

Read More
Android

Fragment

  Fragment 간 데이터 전송 (setArguments() 와 getArguments() 이용)   MainActivity.java (onCreate) FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); Fragment1 fragment1 = new Fragment1(); transaction.replace(R.id.frameLayout, fragment1); transaction.commit(); // 저장   activity_main.xml <ConstraintLayout> <FrameLayout> // id : frameLayout </ConstraintLayout>   fragment_1.xml, fragment_2.xml textview 와 button 한개씩 만든다.   Fragment1.java, Fragment2.java (onCreateView)   View view = inflater.inflate(R.layout.frag1, container, false); TextView textView = view.findViewById(R.id.textView); Button button = view.findViewById(R.id.button); if (getArguments() != null) { String result = getArguments().getString(“fragSend”); textView.setText(result); } button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Bundle bundle = new Bundle(); bundle.putString(“fragSend”,”프레그먼트 데이터 전달…

Read More
Android

      public class MainActivity extends AppCompatActivity { Button btn_start, btn_stop; Thread thread; boolean isThread = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 스레드 시작 btn_start = (Button)findViewById(R.id.btn_start); btn_start.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { isThread = true; thread = new Thread() { public void run() { while(isThread) { try { sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } handler.sendEmptyMessage(0); } } }; thread.start(); } }); // 스레드 종료 btn_stop = (Button)findViewById(R.id.btn_stop); btn_stop.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { isThread = false;…

Read More
Android

Navigation Menu Custom

    activity_main.xml   <androidx.drawerlayout.widget.DrawerLayout 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:id=”@+id/drawer_layout” android:layout_width=”match_parent” android:layout_height=”match_parent” tools:context=”.MainActivity”> <LinearLayout android:layout_width=”match_parent” android:layout_height=”match_parent”> <Button android:id=”@+id/btn_open” android:layout_width=”match_parent” android:layout_height=”wrap_content” android:text=”Open”/> </LinearLayout> <include layout=”@layout/activity_drawer” /> </androidx.drawerlayout.widget.DrawerLayout>       activity_drawer.xml   <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”240dp” android:layout_height=”match_parent” android:layout_gravity=”start” android:background=”#a8a7ff” android:id=”@+id/drawer” android:orientation=”vertical”> <Button android:id=”@+id/btn_close” android:layout_width=”match_parent” android:layout_height=”wrap_content” android:layout_margin=”10dp” android:text=”Close”/> </LinearLayout>       public class MainActivity extends AppCompatActivity { private DrawerLayout drawerLayout; private View drawerView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout); drawerView = (View)findViewById(R.id.drawer); Button btn_open = (Button)findViewById(R.id.btn_open); btn_open.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { drawerLayout.openDrawer(drawerView); } });…

Read More
Android

Dialogs (AlertDialog, DialogFragment)

  # Dialogs (android developers) https://developer.android.com/guide/topics/ui/dialogs#java   # AlertDialog (멈춤보단 천천히라도) https://webnautes.tistory.com/1094   예 아니오 다이얼로그 리스트 다이얼로그 입력 창 예제 다중 선택 다이얼로그 로그인 창 다이얼로그 DialogFragment (Activity에서 호출하는 경우) DialogFragment (Fragment 에서 호출하는 경우)         예 아니오 다이얼로그   AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this); dialog.setTitle(“타이틀”); dialog.setMessage(“콘텐트”); dialog.setPositiveButton(“예”, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(),”예를 선택했습니다.”,Toast.LENGTH_LONG).show(); } }); dialog.setNegativeButton(“아니오”, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(),”아니오를 선택했습니다.”,Toast.LENGTH_LONG).show(); } }); dialog.show();   <?xml version=”1.0″ encoding=”utf-8″?> <RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent”…

Read More
Android

SharedPreferences

  Shared 라는 xml 파일에 저장 (onDestroy에서 사용) SharedPreferences pref = getSharedPreferences(“Shared”, Activity.MODE_PRIVATE); SharedPreferences.Editor editor = pref.edit(); editor.putString(“myText”, value); editor.commit();   불러오기 (OnCreate 에서 사용) SharedPreferences pref = getSharedPreferences(“Shared”, Activity.MODE_PRIVATE); String myText = pref.getGetstring(“myText”, 0);   값 삭제 editor.remove(“myText”);  // myText 라는 키 값 삭제 editor.clear();  // 모두 삭제   // Fragment 는 onCreateView, onDestroyView   [안드로이드] SharedPreference 사용 방법 https://swalloow.tistory.com/59   안드로이드 SharedPreferences 사용법 http://zeany.net/24   홍드로이드 유튜브 (SharedPreferences) https://youtu.be/-2QPmS4OWos   홍드로이드 github https://github.com/hongdroid94  

Read More
Android

Intent

  Intent 로 값 전달하기   // 전달하는 쪽 Intent intent = new Intent(this, ResultActivity.class); myIntent.putExtra(“name”, “Hong gil-dong”); myIntent.putExtra(“age”, 30); startActivity(intent);   // 받는 쪽 Intent intent = getIntent(); Bundle extras = getIntent().getExtras(); String name = extras.getString(“name”); String name = extras.getInt(“age”); or String name = getIntent().getStringExtra(“name”); int age = getIntent().getStringExtra(“age”);   // 키가 있는지 검사 if (intent.hasExtra(“name”)) { }    

Read More
Android

Notification

    public void ShowNotification() { NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); // 채널을 생성하고 시스템에 등록 (오레오 API 26 이상부터는 채널 필요) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { CharSequence name = “My Channel 1”; String description = “My Channel 1 Description”; int importance = NotificationManager.IMPORTANCE_HIGH; // HIGH 옵션은 팝업 표시됨 NotificationChannel channel = new NotificationChannel(“12345”, name, importance); // 12345는 채널 ID channel.setDescription(description); notificationManager.createNotificationChannel(channel); // 채널을 시스템에 등록 } // 알림 생성 NotificationCompat.Builder builder = new NotificationCompat.Builder(this, “12345”) // 12345는 채널 ID .setSmallIcon(R.drawable.ic_launcher_background) .setContentTitle(“My notification”) .setContentText(“Much longer text that cannot fit one line…”)…

Read More