Android

될 때까지 안드로이드 정리 (오준석의 생존코딩) 2

 

 

  1. 14장 뷰페이저 좌우로 밀리는 화면
  2. 15장 환경에 따라 화면 구성하기
  3. 16장 리소스
  4. 17장 브로드캐스트 리시버
  5. 18장 콘텐트 프로바이더
  6. 19장 비동기 처리-1
  7. 19장 비동기 처리-2
  8. 20장 DB를 이용한 데이터 저장과 공유
  9. 22장 네트워크 통신-1 OkHttp
  10. 22장 네트워크 통신-2 Gson
  11. 23장 서비스, 인텐트 서비스, 포그라운드 서비스, 바인드 서비스
  12. 24장 알림과 알람매니저 – 1
  13. 24장 알림과 알람매니저 – 2
  14. 25장 지도를 이용해보자1 – 구글 지도

 

 


 

될 때까지 안드로이드 #11 [14장 뷰페이저 좌우로 밀리는 화면]

 

# 메뉴에서 Fragment 3개를 새로 만든다.

 

MyPagerAdapter.java

 

public class MyPagerAdapter extends FragmentPagerAdapter {
    private ArrayList<Fragment> mData;

    public MyPagerAdapter(FragmentManager fm) {
        super(fm);

        mData = new ArrayList<>();
        mData.add(new BlankFragment());
        mData.add(new ItemFragment());
        mData.add(new SettingsFragment());
    }

    @Override
    public Fragment getItem(int position) {
        return mData.get(position);
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return position + " 번째";
    }
}

 

 

 

public class MainActivity extends AppCompatActivity {

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

        ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
        MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tab);
        tabLayout.setupWithViewPager(viewPager);
    }
}

 

 

 

<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tab"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabSelectedTextColor="#0000ff"
        app:tabTextColor="#ff0000" />

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

 

 

ListFragment 에서 콜백 인터페이스 만들기

 

MyItemRecyclerViewAdapter.java

 

private final ItemFragment.OnListFragmentInteractionListener mListener;

public MyItemRecyclerViewAdapter(List<DummyItem> items, ItemFragment.OnListFragmentInteractionListener listener) {
    mValues = items;
    mListener = listener;
}
public void onBindViewHolder(final ViewHolder holder, int position) {
    holder.mView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (null != mListener) {
                mListener.onListFragmentInteraction(holder.mItem);
            }
        }
    });

 

 

ItemFragment.java

 

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
...
        recyclerView.setAdapter(new MyItemRecyclerViewAdapter(DummyContent.ITEMS, mListener));
...
}

public interface OnListFragmentInteractionListener {
    // TODO: Update argument type and name
    void onListFragmentInteraction(DummyContent.DummyItem item);
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnListFragmentInteractionListener) {
        mListener = (OnListFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnListFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

 

 

MainActivity.java

 

@Override
public void onListFragmentInteraction(DummyContent.DummyItem item) {
    Toast.makeText(this, item.toString(), Toast.LENGTH_SHORT).show();
}

 

 

 


 

될 때까지 안드로이드 #12 [15장 환경에 따라 화면 구성하기]

 

activity_main.xml

 

<LinearLayout 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"
android:id="@+id/activity_main"
tools:context=".MainActivity">

<FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>

 

 

activity_main.xml (large)

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <fragment
        android:id="@+id/headlines_fragment"
        android:name="com.example.dynamicui.HeadlinesFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        tools:layout="@android:layout/simple_list_item_1" />

    <fragment
        android:id="@+id/article_fragment"
        android:name="com.example.dynamicui.ArticleFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        tools:layout="@layout/fragment_article" />

</LinearLayout>

 

 

public class Articles {

    static String[] Headlines = {
            "기사제목 1", "기사제목 2"
    };

    static String[] Articles = {
            "이것은 기사1의 내용입니다",
            "이것은 기사2의 내용입니다"
    };
}

 

 

 

public class ArticleFragment extends Fragment {
    public static final String ARG_POSITION = "position";
    private int mCurrentPosition = -1;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        // 화면이 회전되면 이전에 선택된 위치를 복원
        if (savedInstanceState != null) {
            mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
        }

        // 화면 레이아웃은 TextView 하나만 있는 레이아웃을 사용
        return inflater.inflate(R.layout.fragment_article, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        Bundle args = getArguments();
        if (args != null) {
            // 프래그먼트가 생성되었을 경우
            updateArticleView(args.getInt(ARG_POSITION));
        } else if (mCurrentPosition != -1) {
            // 화면 회전 등의 경우
            updateArticleView(mCurrentPosition);
        }
    }

    // 선택된 기사를 표시
    public void updateArticleView(int position) {
        TextView article = (TextView) getView().findViewById(R.id.article_text);
        article.setText(Articles.Articles[position]);
        mCurrentPosition = position;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        // 화면이 회전될 때, 선택된 위치를 저장
        outState.putInt(ARG_POSITION, mCurrentPosition);
    }
}

 

 

 

public class HeadlinesFragment extends ListFragment {

    interface OnHeadlineSelectedListener {
        void onHeadlineSelected(int position);
    }

    private OnHeadlineSelectedListener mListener;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setListAdapter(new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, Articles.Headlines));
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        try {
            mListener = (OnHeadlineSelectedListener) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString() +
                    " must implement OnHeadlineSelectedListener");
        }
    }

    @Override
    public void onListItemClick(@NonNull ListView l, @NonNull View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        if (mListener != null) {
            mListener.onHeadlineSelected(position);
        }
    }
}

 

 

 

public class MainActivity extends AppCompatActivity implements HeadlinesFragment.OnHeadlineSelectedListener {

    public static final String TAG = "MainActivity";

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

        // layout-large 의 레이아웃에는 fragment_container 가 없음
        if (findViewById(R.id.fragment_container) != null) {
            // 화면 회전시에 HeadlinesFragment가 재생성 되는 것을 방지
            if (savedInstanceState == null) {
                HeadlinesFragment headlinesFragment = new HeadlinesFragment();

                // headlinesFragment를 R.id.fragment_container 영역에 추가
                getSupportFragmentManager().beginTransaction()
                        .add(R.id.fragment_container, headlinesFragment)
                        .commit();
            }
        }
    }

    // HeadlinesFragment의 기사제목이 선택되었을 경우에 호출됨
    @Override
    public void onHeadlineSelected(int position) {
        ArticleFragment articleFragment = (ArticleFragment)
                getSupportFragmentManager().findFragmentById(R.id.article_fragment);

        // layout-large 의 경우 null이 아님
        if (articleFragment == null) {

            // ArticleFragment프래그먼트를 생성
            ArticleFragment newArticleFragment = new ArticleFragment();
            // Argument로 기사 번호를 전달
            Bundle args = new Bundle();
            args.putInt(ArticleFragment.ARG_POSITION, position);
            newArticleFragment.setArguments(args);

            // R.id.fragment_container 아이디를 가진 영역의
            // 프래그먼트를 articleFragment로 교체하고
            // 프래그먼트 매니저의 BackStack에 쌓는다
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.fragment_container, newArticleFragment)
                    .addToBackStack(null)
                    .commit();
        } else {
            articleFragment.updateArticleView(position);
        }
    }
}

 

 

 


 

될 때까지 안드로이드 #13 [16장 리소스]

 

New -> values resource file -> strings.xml -> Local -> ko

또는

Open Editor

 

———————————————-

layout_alignParentStart

layout_alignParentLeft

오른쪽에서 왼쪽으로 글을 쓰는 국가에서 layout_alignParentStart 는 오른쪽으로 맞춰진다.

 

 

 

 

 


 

될 때까지 안드로이드 #14 [17장 브로드캐스트 리시버]

 

<application
    ....
    <receiver
        android:name=".MyReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter android:priority="9999">
            <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
            <action android:name="com.example.broadcastreceiverexam.broadcast.ACTION_MY_BROADCAST" />
            <action android:name="com.example.my_broadcast" />
            <action android:name="android.intent.action.BATTERY_LOW" />
        </intent-filter>
    </receiver>

 

 

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:onClick="sendMyBroadcast"
        android:text="나만의 방송"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

 

 

public class MainActivity extends AppCompatActivity {

    private BroadcastReceiver mReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mReceiver = new MyReceiver();
    }

    @Override
    protected void onResume() {
        super.onResume();
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_POWER_CONNECTED);
        filter.addAction(MyReceiver.MY_ACTION);
        filter.setPriority(100);
        registerReceiver(mReceiver, filter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(mReceiver);
    }

    public void sendMyBroadcast(View view) {
        Intent intent = new Intent(MyReceiver.MY_ACTION);
        sendBroadcast(intent);
    }
}

 

 

public class MyReceiver extends BroadcastReceiver {
    public static final String MY_ACTION = "com.example.broadcastreceiverexam.action.ACTION_MY_BROADCAST";

    @Override
    public void onReceive(Context context, Intent intent) {
        if (Intent.ACTION_POWER_CONNECTED.equals(intent.getAction())) {
            Toast.makeText(context, "전원 연결 됨", Toast.LENGTH_SHORT).show();
        } else if (MY_ACTION.equals(intent.getAction())) {
            Toast.makeText(context, "이 방송은 나만의 방송", Toast.LENGTH_SHORT).show();

            // 이후의 브로드캐스트의 전파를 막기
            abortBroadcast();
        }
    }
}

 

 


 

될 때까지 안드로이드 #15 [18장 콘텐트 프로바이더]

 

 

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
------------------------------------------------------------------------------------
    compileSdkVersion 26
    minSdkVersion 21
    targetSdkVersion 22 // 23부터는 권한을 체크하기 때문에 22로 설정한다.
------------------------------------------------------------------------------------
implementation 'com.github.bumptech.glide:glide:4.11.0'

 

 

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.cursoradapterexam.MainActivity">

    <GridView
        android:id="@+id/photo_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:horizontalSpacing="4dp"
        android:numColumns="2"
        android:verticalSpacing="4dp" />
</LinearLayout>

 

 

item_photo.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/photo_image"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:scaleType="centerCrop" />

</LinearLayout>

 

 

 

public class MyCursorAdapter extends CursorAdapter {
    public MyCursorAdapter(Context context, Cursor c) {
        super(context, c, false);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return LayoutInflater.from(context).inflate(R.layout.item_photo, parent, false);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        ImageView imageView = (ImageView) view.findViewById(R.id.photo_image);

        // 사진 경로 가지고 오기 (URI)
        String uri = cursor.getString
                (cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));

        // 사진을 이미지뷰에 표시하기
        // imageView.setImageURI(Uri.parse(uri));
        Glide.with(context).load(uri).into(imageView);  // 글라이드를 사용하면 비동기로 작동되므로 더 효율적임. 
    }
}

 

 

 

public class MainActivity extends AppCompatActivity {

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

        // 뷰
        GridView photoListView = (GridView) findViewById(R.id.photo_list);

        // 사진 데이터
        Cursor cursor = getContentResolver().query
                (MediaStore.Images.Media.EXTERNAL_CONTENT_URI,    // From
                        null,   // Select 절
                        null,   // Where 절
                        null,   // Where 절
                        MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC");// Order By

        // 어댑터
        MyCursorAdapter adapter = new MyCursorAdapter(this, cursor);
        photoListView.setAdapter(adapter);

        // 클릭 이벤트 처리
        photoListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // 클릭한 부분의 cursor 데이타
                Cursor cursor = (Cursor) parent.getAdapter().getItem(position);
                String path = cursor.getString
                        (cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));

                Toast.makeText(MainActivity.this, "사진 경로 : " + path,
                        Toast.LENGTH_SHORT).show();
            }
        });
    }
}

 

 


 

될 때까지 안드로이드 #16 [19장 비동기 처리-1]

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.threadexam.MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="진행률 : " />

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="download"
        android:text="다운로드 시작" />

    <Button
        android:id="@+id/cancel_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="취소" />

</LinearLayout>

 

 

핸들러 이용
public class MainActivity extends AppCompatActivity {

    private TextView mTextView;
    private ProgressBar mProgressBar;
    private Handler mHandler = new Handler();

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

        mTextView = findViewById(R.id.textView);
        mProgressBar = findViewById(R.id.progressBar);
    }

    public void download(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                // 오래 걸리는 일
                for (int i = 0; i <= 100; i++) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    final int percent = i;
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            // UI 갱신
                            mTextView.setText(percent + "%");
                            mProgressBar.setProgress(percent);
                        }
                    });
                }
            }
        }).start();
    }
}

 

 

runOnUiThread
public class MainActivity extends AppCompatActivity {

    private TextView mTextView;
    private ProgressBar mProgressBar;

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

        mTextView = findViewById(R.id.textView);
        mProgressBar = findViewById(R.id.progressBar);
    }

    public void download(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                // 오래 걸리는 일
                for (int i = 0; i <= 100; i++) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    final int percent = i;
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            // UI 갱신
                            mTextView.setText(percent + "%");
                            mProgressBar.setProgress(percent);
                        }
                    });
                }
            }
        }).start();
    }
}

 

 

뷰 내장 핸들러 사용
public class MainActivity extends AppCompatActivity {

    private TextView mTextView;
    private ProgressBar mProgressBar;

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

        mTextView = findViewById(R.id.textView);
        mProgressBar = findViewById(R.id.progressBar);
    }

    public void download(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                // 오래 걸리는 일
                for (int i = 0; i <= 100; i++) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    final int percent = i;

                    mTextView.post(new Runnable() {
                        @Override
                        public void run() {
                            // UI 갱신
                            mTextView.setText(percent + "%");
                            mProgressBar.setProgress(percent);
                        }
                    });
                }
            }
        }).start();
    }
}

 

 

핸들러의 postDelayed()

public class MainActivity extends AppCompatActivity {

    private TextView mTextView;
    private ProgressBar mProgressBar;

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

        mTextView = findViewById(R.id.textView);
        mProgressBar = findViewById(R.id.progressBar);
    }

    public void download(View view) {
        Toast.makeText(this, "5초 후에 알림", Toast.LENGTH_SHORT).show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(MainActivity.this, "5초 지났음", Toast.LENGTH_SHORT).show();
            }
        }, 5000);
    }
}

 

 

AsyncTask
public class MainActivity extends AppCompatActivity {

    private TextView mTextView;
    private ProgressBar mProgressBar;

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

        mTextView = findViewById(R.id.textView);
        mProgressBar = findViewById(R.id.progressBar);
    }

    public void download(View view) {
        new DownLoadTask().execute();
    }

    class DownLoadTask extends AsyncTask<Void, Integer, Void> {

        @Override
        protected Void doInBackground(Void... voids) {
            for (int i = 0; i <= 100; i++) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                final int percent = i;

                // UI 갱신 요청
                publishProgress(percent);
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            // UI 갱신 요청
            mTextView.setText(values[0] + "%");
            mProgressBar.setProgress(values[0]);
        }
    }
}

 

 

Cancel 처리
public class MainActivity extends AppCompatActivity {

    private TextView mTextView;
    private ProgressBar mProgressBar;
    private DownLoadTask mDownLoadTask;

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

        mTextView = findViewById(R.id.textView);
        mProgressBar = findViewById(R.id.progressBar);

        findViewById(R.id.cancel_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mDownLoadTask != null && !mDownLoadTask.isCancelled()) {
                    mDownLoadTask.cancel(true);
                }
            }
        });
    }

    public void download(View view) {
        mDownLoadTask = new DownLoadTask();
        mDownLoadTask.execute();
    }

    class DownLoadTask extends AsyncTask<Void, Integer, Void> {

        @Override
        protected Void doInBackground(Void... voids) {
            for (int i = 0; i <= 100; i++) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                final int percent = i;

                // UI 갱신 요청
                publishProgress(percent);

                // 취소 됨
                if (isCancelled()) {
                    break;
                }
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            // UI 갱신 요청
            mTextView.setText(values[0] + "%");
            mProgressBar.setProgress(values[0]);
        }

        @Override
        protected void onCancelled(Void aVoid) {
            Toast.makeText(MainActivity.this, "취소 됨", Toast.LENGTH_SHORT).show();
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            Toast.makeText(MainActivity.this, "완료 됨", Toast.LENGTH_SHORT).show();
        }
    }
}

 

AsyncTask 는 재활용 하면 안되고 사용할 때마다 새 객체를 계속 만들어야함.

mDownLoadTask.execute();  // 순차적 처리 (버튼을 계속 눌렀을 때)

mDownLoadTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, null); // 병렬 처리

 


 

될 때까지 안드로이드 #17 [19장 비동기 처리-2]

 

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.countexam.MainActivity">

    <TextView
        android:id="@+id/count"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="0"
        android:textSize="80dp" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="start"
        android:text="시작" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="clear"
        android:text="초기화" />

</LinearLayout>

 

 

 

public class MainActivity extends AppCompatActivity {
    private TextView mTextView;
    private CountTask mTask;

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

        mTextView = (TextView) findViewById(R.id.count);
    }

    public void start(View view) {
        mTask = new CountTask();
        mTask.execute(0);
    }

    public void clear(View view) {
        mTask.cancel(true);
        mTextView.setText("0");
    }

    public class CountTask extends AsyncTask<Integer, Integer, Integer> {

        @Override
        protected Integer doInBackground(Integer... params) {
            do {
                // 1초 쉬기
                try {
                    Thread.sleep(1000);
                    // 0 부터 10 까지 1씩 증가
                    params[0]++;
                    // 증가된 값을 텍스트뷰에 표시해 줘
                    publishProgress(params[0]);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } while (params[0] < 10);
            return params[0];
        }

        @Override
        protected void onProgressUpdate(Integer... progress) {
            // 텍스트뷰에 증가된 값 표시
            mTextView.setText(String.valueOf(progress[0]));
        }

        @Override
        protected void onPostExecute(Integer result) {
            // 종료시 마지막 값 텍스트뷰에 표시
            mTextView.setText(String.valueOf(result));
        }
    }
}

 

 

 


 

될 때까지 안드로이드 #18 [20장 DB를 이용한 데이터 저장과 공유]

 

    </activity>

    <provider
        android:name=".MemoProvider"
        android:authorities="com.example.mysqliteexamapplication.provider"
        android:enabled="true"
        android:exported="true"></provider>
</application>

 

 

 

implementation 'com.google.android.material:material:1.2.0'

 

 

Vector Asset -> add -> drawable/ic_add_black_24dp.xml

 

 

activity_main.xml
<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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/memo_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:src="@drawable/ic_add_black_24dp"
        app:fabSize="auto"
        app:useCompatPadding="true" />
</RelativeLayout>

 

 

activity_memo.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_memo"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/title_edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="제목"
        android:maxLines="1" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#cfcfcf">

        <EditText
            android:id="@+id/contents_edit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            android:hint="내용" />
    </ScrollView>

</LinearLayout>

 

 

 

public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor> {
    private static final int REQUEST_CODE_INSERT = 1000;
    private MemoAdapter mAdapter;

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

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this,
                        MemoActivity.class));

            }
        });

        ListView listView = (ListView) findViewById(R.id.memo_list);

//        MemoDbHelper dbHelper = MemoDbHelper.getInstance(this);
//        Cursor cursor = dbHelper.getReadableDatabase()
//                .query(MemoContract.MemoEntry.TABLE_NAME,
//                        null, null, null, null, null, null);

        mAdapter = new MemoAdapter(this, null);
        listView.setAdapter(mAdapter);

        // 리스트 클릭시 메모 내용 표시
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Intent intent = new Intent(MainActivity.this, MemoActivity.class);
                // 클릭한 시점의 아이템을 얻음
                Cursor cursor = (Cursor) mAdapter.getItem(position);
                // 커서에서 제목과 내용을 얻음
                String title = cursor.getString(cursor.getColumnIndexOrThrow
                        (MemoContract.MemoEntry.COLUMN_NAME_TITLE));
                String contents = cursor.getString(cursor.getColumnIndexOrThrow
                        (MemoContract.MemoEntry.COLUMN_NAME_CONTENTS));
                // 인텐트에 id와 함께 저장
                intent.putExtra("id", id);
                intent.putExtra("title", title);
                intent.putExtra("contents", contents);
                // MemoActivity 를 시작
                startActivity(intent);
            }
        });

        // 아이템 롱 클릭 이벤트 정의
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position,
                                           long id) {
                final long deleteId = id;
                // 삭제할 것인지 다이얼로그 표시
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("메모 삭제");
                builder.setMessage("메모를 삭제하시겠습니까?");
                builder.setPositiveButton("삭제", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        SQLiteDatabase db =
                                MemoDbHelper.getInstance(MainActivity.this).getWritableDatabase();
//                        int deletedCount = db.delete(MemoContract.MemoEntry.TABLE_NAME,
//                                MemoContract.MemoEntry._ID + " = " + deleteId, null);
                        int deletedCount = getContentResolver().delete(MemoProvider.CONTENT_URI,
                                MemoContract.MemoEntry._ID + " = " + deleteId, null);

                        if (deletedCount == 0) {
                            Toast.makeText(MainActivity.this, "삭제에 문제가 발생하였습니다",
                                    Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(MainActivity.this, "메모가 삭제되었습니다",
                                    Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                builder.setNegativeButton("취소", null);
                builder.show();
                return true;
            }
        });

        getSupportLoaderManager().initLoader(0, null, this);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        return new CursorLoader(this, MemoProvider.CONTENT_URI, null, null, null,
                MemoContract.MemoEntry._ID + " DESC");

    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        mAdapter.swapCursor(data);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        mAdapter.swapCursor(null);
    }

    private static class MemoAdapter extends CursorAdapter {
        public MemoAdapter(Context context, Cursor c) {
            super(context, c, false);
        }

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            return LayoutInflater.from(context)
                    .inflate(android.R.layout.simple_list_item_1, parent, false);
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            TextView titleText = (TextView) view.findViewById(android.R.id.text1);
            titleText.setText(cursor.getString(cursor.getColumnIndexOrThrow
                    (MemoContract.MemoEntry.COLUMN_NAME_TITLE)));
        }
    }
}

 

 

 

public class MemoActivity extends AppCompatActivity {
    private EditText mTitleEditText;
    private EditText mContentsEditText;

    private long mMemoId = -1;

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

        mTitleEditText = (EditText) findViewById(R.id.title_edit);
        mContentsEditText = (EditText) findViewById(R.id.contents_edit);

        Intent intent = getIntent();
        if (intent != null) {
            mMemoId = intent.getLongExtra("id", -1);
            String title = intent.getStringExtra("title");
            String contents = intent.getStringExtra("contents");
            mTitleEditText.setText(title);
            mContentsEditText.setText(contents);
        }
    }

    @Override
    public void onBackPressed() {
        // DB 에 저장하는 처리
        String title = mTitleEditText.getText().toString();
        String contents = mContentsEditText.getText().toString();

        ContentValues contentValues = new ContentValues();
        contentValues.put(MemoContract.MemoEntry.COLUMN_NAME_TITLE, title);
        contentValues.put(MemoContract.MemoEntry.COLUMN_NAME_CONTENTS, contents);

//        SQLiteDatabase db = MemoDbHelper.getInstance(this).getWritableDatabase();
        if (mMemoId == -1) {
//            // DB 에 저장하는 처리
//            long newRowId = db.insert(MemoContract.MemoEntry.TABLE_NAME, null, contentValues);
//
//            if (newRowId == -1) {
            Uri uri = getContentResolver().insert(MemoProvider.CONTENT_URI, contentValues);
            if (uri == null) {

                Toast.makeText(this, "저장에 문제가 발생하였습니다",
                        Toast.LENGTH_SHORT).show();
            }
        } else {
            // 기존 메모 내용을 업데이트 처리
//            int count = db.update(MemoContract.MemoEntry.TABLE_NAME, contentValues,
//                    MemoContract.MemoEntry._ID + " = " + mMemoId, null);
            int count = getContentResolver().update(MemoProvider.CONTENT_URI,
                    contentValues, MemoContract.MemoEntry._ID + " = " + mMemoId, null);


            if (count == 0) {
                Toast.makeText(this, "수정에 문제가 발생하였습니다",
                        Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "메모가 수정되었습니다", Toast.LENGTH_SHORT).show();
            }
        }

        // 뒤로 가기의 원래의 동작이 실행 됨
        super.onBackPressed();
    }
}

 

 

 

public final class MemoContract {

    // 인스턴스화 금지
    private MemoContract() {
    }

    // 테이블 정보를 내부 클래스로 정의
    public static class MemoEntry implements BaseColumns {
        public static final String TABLE_NAME = "memo";
        public static final String COLUMN_NAME_TITLE = "title";
        public static final String COLUMN_NAME_CONTENTS = "contents";
    }
}

 

 

 

public class MemoDbHelper extends SQLiteOpenHelper {
    private static MemoDbHelper sInstance;

    // DB의 버전으로 1부터 시작하고 스키마가 변경될 때 숫자를 올린다
    private static final int DB_VERSION = 1;
    // DB 파일명
    private static final String DB_NAME = "Memo.db";

    // 테이블 생성 SQL문
    private static final String SQL_CREATE_ENTRIES =
            String.format("CREATE TABLE %s (%s INTEGER PRIMARY KEY AUTOINCREMENT, %s TEXT, %s TEXT)",
                    MemoContract.MemoEntry.TABLE_NAME,
                    MemoContract.MemoEntry._ID,
                    MemoContract.MemoEntry.COLUMN_NAME_TITLE,
                    MemoContract.MemoEntry.COLUMN_NAME_CONTENTS);
    // 테이블 삭제 SQL문
    private static final String SQL_DELETE_ENTRIES =
            "DROP TABLE IF EXISTS " + MemoContract.MemoEntry.TABLE_NAME;

    // 팩토리 메서드
    public static synchronized MemoDbHelper getInstance(Context context) {
        // 액티비티의 context가 메모리 릭(leak)을 발생할 수 있으므로
        // application context를 사용하는 것이 좋다
        if (sInstance == null) {
            sInstance = new MemoDbHelper(context.getApplicationContext());
        }
        return sInstance;
    }

    // 생성자를 private로 직접 인스턴스화를 방지하고
    // getInstance()를 통해 인스턴스를 얻어야 함
    private MemoDbHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(SQL_CREATE_ENTRIES);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // DB 스키마가 변경될 때 여기서 데이터를 백업하고
        // 테이블을 삭제 후 재생성 및 데이터 복원 등을 한다
        db.execSQL(SQL_DELETE_ENTRIES);
        onCreate(db);
    }
}

 

 

 

public class MemoProvider extends ContentProvider {

    // 프로바이더 이름
    private static final String AUTHORITY = "com.example.mysqliteexamapplication.provider";

    // content://com.example.databaseexam.provider/memo
    // 프로바이더의 memo 테이블
    public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/"
            + MemoContract.MemoEntry.TABLE_NAME);

    // 1개의 아이템 요청 MIME 타입
    public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.com.example.mysqliteexamapplication.provider." + MemoContract.MemoEntry.TABLE_NAME;
    // 여러 개의 아이템 요청 MIME 타입
    public static final String CONTENT_ALL_TYPE =
            "vnd.android.cursor.dir/vnd.com.example.mysqliteexamapplication.provider." +
                    MemoContract.MemoEntry.TABLE_NAME;

    // UriMatcher 객체 생성
    private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

    public static final int ALL = 1;
    public static final int ITEM = 2;

    static {
        // content://com.example.databaseexam.provider/memo
        sUriMatcher.addURI(AUTHORITY, MemoContract.MemoEntry.TABLE_NAME, ALL);
        // content://com.example.databaseexam.provider/memo/1 (#은 모든 숫자와 대응)
        sUriMatcher.addURI(AUTHORITY, MemoContract.MemoEntry.TABLE_NAME + "/#", ITEM);
    }

    private MemoDbHelper mMemoDbHelper;


    public MemoProvider() {
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        // URI 타입에 따라 조건 여부 결정
        switch (sUriMatcher.match(uri)) {
            case ALL:
                break;

            case ITEM:
                // uri의 # 뒤의 숫자 (_id)만 뽑아서 조건문을 완성
                selection = "_id=" + ContentUris.parseId(uri);
                selectionArgs = null;
                break;

            case UriMatcher.NO_MATCH:
                return 0;
        }

        SQLiteDatabase db = mMemoDbHelper.getWritableDatabase();
        int deleteCount = db.delete(MemoContract.MemoEntry.TABLE_NAME,
                selection,
                selectionArgs);

        if (deleteCount > 0) {
            // 상태가 변경됨을 ContentResolver에 통지
            getContext().getContentResolver().notifyChange(uri, null);
        }


        return deleteCount;
    }

    @Override
    public String getType(Uri uri) {
        // 이 프로바이더가 처리 할 수 있는 패턴인지 검사
        switch (sUriMatcher.match(uri)) {
            case ALL:
                return CONTENT_ALL_TYPE;
            case ITEM:
                return CONTENT_ITEM_TYPE;
            default:
                throw new IllegalArgumentException("Unknown URI " + uri);
        }
    }


    @Override
    public Uri insert(Uri uri, ContentValues values) {
        switch (sUriMatcher.match(uri)) {
            case ALL:
                long id = mMemoDbHelper.getWritableDatabase()
                        .insert(MemoContract.MemoEntry.TABLE_NAME, null, values);
                if (id > 0) {
                    // content://com.example.databaseexam.provider/#[id]
                    Uri returnUri = ContentUris.withAppendedId(CONTENT_URI, id);
                    // 상태가 변경됨을 ContentResolver에 통지
                    getContext().getContentResolver().notifyChange(returnUri, null);
                    return returnUri;
                }
                break;
        }
        return null;

    }

    @Override
    public boolean onCreate() {
        // 메모 DB 헬퍼의 인스턴스 초기화
        mMemoDbHelper = MemoDbHelper.getInstance(getContext());
        return true;

    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        switch (sUriMatcher.match(uri)) {
            case ALL:
                break;

            case ITEM:
                // uri의 #뒤의 숫자 (_id)만 뽑아서 조건문을 완성
                selection = "_id=" + ContentUris.parseId(uri);
                selectionArgs = null;
                break;
            default:
                throw new IllegalArgumentException("Unknown URI " + uri);
        }

        SQLiteDatabase database = mMemoDbHelper.getReadableDatabase();
        Cursor cursor = database.query(MemoContract.MemoEntry.TABLE_NAME,
                projection,
                selection,
                selectionArgs,
                null,
                null,
                sortOrder);

        // 커서를 감시대상으로 설정
        cursor.setNotificationUri(getContext().getContentResolver(), uri);
        return cursor;

    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        switch (sUriMatcher.match(uri)) {
            case ALL:
                break;

            case ITEM:
                // uri의 #뒤의 숫자 (_id)만 뽑아서 조건문을 완성
                selection = "_id=" + ContentUris.parseId(uri);
                selectionArgs = null;
                break;

            case UriMatcher.NO_MATCH:
                return 0;
        }

        SQLiteDatabase db = mMemoDbHelper.getWritableDatabase();

        int update = db.update(MemoContract.MemoEntry.TABLE_NAME,
                values,
                selection,
                selectionArgs);

        if (update > 0) {
            // 상태가 변경됨을 ContentResolver 에 통지
            getContext().getContentResolver().notifyChange(uri, null);
        }

        return update;
    }
}

 

 

 

 


 

될 때까지 안드로이드 #19 [22장 네트워크 통신-1 OkHttp]

 

<uses-permission android:name="android.permission.INTERNET" />

 

implementation 'com.squareup.okhttp3:okhttp:3.10.0'
implementation 'com.google.code.gson:gson:2.8.5'

 

 

activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp">

    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:listitem="@layout/item_weather" />

</LinearLayout>

 

 

item_weather.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/country_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="국가" />

    <TextView
        android:id="@+id/weather_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="날씨" />

    <TextView
        android:id="@+id/temperature_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="기온" />

</LinearLayout>

 

 

 

public class MainActivity extends AppCompatActivity {

    private ListView mWeatherListView;

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

        mWeatherListView = (ListView) findViewById(R.id.list_view);

        // 소스를 확인하고 싶은 사이트 주소
        new HttpAsyncTask().execute("https://goo.gl/eIXu9l");
    }

    private class HttpAsyncTask extends AsyncTask<String, Void, List<Weather>> {
        private final String TAG = HttpAsyncTask.class.getSimpleName();

        // OkHttp 클라이언트
        OkHttpClient client = new OkHttpClient();

        @Override
        protected List<Weather> doInBackground(String... params) {
            List<Weather> weatherList = new ArrayList<>();
            String strUrl = params[0];
            try {
                // 요청
                Request request = new Request.Builder()
                        .url(strUrl)
                        .build();
                // 응답
                Response response = client.newCall(request).execute();

                Gson gson = new Gson();

                // import java.lang.reflect.Type
                Type listType = new TypeToken<ArrayList<Weather>>() {
                }.getType();
                weatherList = gson.fromJson(response.body().string(), listType);

                Log.d(TAG, "onCreate: " + weatherList.toString());
            } catch (IOException e) {
                e.printStackTrace();
            }

            return weatherList;
        }

        @Override
        protected void onPostExecute(List<Weather> weatherList) {
            super.onPostExecute(weatherList);

            if (weatherList != null) {
                Log.d("HttpAsyncTask", weatherList.toString());
                WeatherAdapter adapter = new WeatherAdapter(weatherList);
                mWeatherListView.setAdapter(adapter);
            }
        }
    }
}

 

 

 

public class Weather {
    private String country;
    private String weather;
    private String temperature;

    public Weather(String country, String weather, String temperature) {
        this.country = country;
        this.weather = weather;
        this.temperature = temperature;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getTemperature() {
        return temperature;
    }

    public void setTemperature(String temperature) {
        this.temperature = temperature;
    }

    public String getWeather() {
        return weather;
    }

    public void setWeather(String weather) {
        this.weather = weather;
    }

    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("Weather{");
        sb.append("country='").append(country).append('\'');
        sb.append(", weather='").append(weather).append('\'');
        sb.append(", temperature='").append(temperature).append('\'');
        sb.append('}');
        return sb.toString();
    }
}

 

 

 

public class WeatherAdapter extends BaseAdapter {
    private final List<Weather> mList;

    public WeatherAdapter(List<Weather> list) {
        mList = list;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_weather, parent, false);

            holder = new ViewHolder();
            holder.country = (TextView) convertView.findViewById(R.id.country_text);
            holder.weather = (TextView) convertView.findViewById(R.id.weather_text);
            holder.temperature = (TextView) convertView.findViewById(R.id.temperature_text);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Weather weather = (Weather) getItem(position);
        holder.country.setText(weather.getCountry());
        holder.weather.setText(weather.getWeather());
        holder.temperature.setText(weather.getTemperature());

        return convertView;
    }

    // 뷰 홀더 패턴
    static class ViewHolder {
        TextView country;
        TextView weather;
        TextView temperature;
    }
}

 

 

 


 

될 때까지 안드로이드 #20 [22장 네트워크 통신-2 Gson]

 

 

try {
    JSONArray jsonArray = new JSONArray(response.body().string());
    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject jsonObject = jsonArray.getJSONObject(i);
        String country = jsonObject.getString("country");
        String weather = jsonObject.getString("weather");
        String temperature = jsonObject.getString("temperature");
        Weather w = new Weather(country, weather, temperature);
        weatherList.add(w);
    }
} catch (IOException e) {
    e.printStackTrace();
} catch (JSONException e) {
    e.printStackTrace();
}

 

 

Gson 으로 쉽게 JSON 다루기
Gson gson = new Gson();

Type listType = new TypeToken<ArrayList<Weather>>() {}.getType();

weatherList = gson.fromJson(response.body().string(), listType);

 

 

 


 

될 때까지 안드로이드 #21 [23장, 24장 서비스, 인텐트 서비스, 포그라운드 서비스, 바인드 서비스]

 

 

<service
    android:name=".MyIntentService"
    android:exported="false"></service>
<service
    android:name=".MyService"
    android:enabled="true"
    android:exported="true" />

 

 

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onStartService"
        android:text="서비스 시작" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onStopService"
        android:text="서비스 중지" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onStartIntentService"
        android:text="인텐트 서비스 시작" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onStartForegroundService"
        android:text="포그라운드 서비스 시작" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="getCountValue"
        android:text="카운팅 값 출력" />

</LinearLayout>

 

 

 

public class MainActivity extends AppCompatActivity {
    public static final String TAG = MainActivity.class.getSimpleName();

    private MyService mService;
    private boolean mBound;

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

    public void onStartService(View view) {
        Intent intent = new Intent(this, MyService.class);
        startService(intent);
    }

    // 서비스는 메인 쓰레드로 실행되고 자동 종료되지 않으므로 종료 코드를 작성해야한다.
    public void onStopService(View view) {
        Intent intent = new Intent(this, MyService.class);
        stopService(intent);
    }

    // 인텐트서비스는 쓰레드로 실행되고 자동 종료됨
    public void onStartIntentService(View view) {
        Intent intent = new Intent(this, MyIntentService.class);
        startService(intent);
    }

    public void onStartForegroundService(View view) {
        Intent intent = new Intent(this, MyService.class);
        intent.setAction("startForeground");

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            startForegroundService(intent);
        } else {
            startService(intent);
        }
    }

    public void getCountValue(View view) {
        if (mBound) {
            Toast.makeText(this, "카운팅 : " + mService.getCount(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onStart() {
        super.onStart();

        // 서비스와 연결
        Intent intent = new Intent(this, MyService.class);
        bindService(intent, mConnection, BIND_AUTO_CREATE);
    }

    @Override
    protected void onStop() {
        super.onStop();

        // 서비스와 연결 해제
        if (mBound) {
            unbindService(mConnection);
            mBound = false;
        }
    }

    /**
     * bindService() 를 통해 서비스와 연결될 때의 콜백 정의
     */
    private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            // MyBinder와 연결될 것이며 IBinder 타입으로 넘어오는 것을 캐스팅하여 사용
            MyService.MyBinder binder = (MyService.MyBinder) service;
            mService = binder.getService();
            mBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            // 예기치 않은 종료
        }
    };
}

 

 

 

// 쓰레드로 돌아가고 자동 종료됨
public class MyIntentService extends IntentService {

    public MyIntentService() {
        super("MyIntentService");
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        for (int i = 0; i < 5; i++) {
            try {
                // 1초 마다 쉬기
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
            // 1초 마다 로그 남기기
            Log.d("MyIntentService", "인텐트 서비스 동작 중 " + i);
        }
    }
}

 

 

 

public class MyService extends Service {
    private static final String TAG = MyService.class.getSimpleName();

    private Thread mThread;
    private int mCount = 0;

    public MyService() {
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if ("startForeground".equals(intent.getAction())) {
            // 포그라운드 서비스 시작
            startForegroundService();

        } else if (mThread == null) {
            // 스레드 초기화 및 시작
            mThread = new Thread("My Thread") {
                @Override
                public void run() {
                    for (int i = 0; i < 100; i++) {
                        try {
                            mCount++;
                            // 1초 마다 쉬기
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            // 스레드에 인터럽트가 걸리면
                            // 오래 걸리는 처리 종료
                            break;
                        }
                        // 1초 마다 로그 남기기
                        Log.d(TAG, "서비스 동작 중 " + mCount);
                    }
                }
            };
            mThread.start();
        }
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        Log.d(TAG, "onDestroy: ");

        // stopService 에 의해 호출 됨
        // 스레드를 정지시킴
        if (mThread != null) {
            mThread.interrupt();
            mThread = null;
        }

        super.onDestroy();
    }

    // MyService의 레퍼런스를 반환하는 Binder 객체
    private IBinder mBinder = new MyBinder();

    public class MyBinder extends Binder {
        public MyService getService() {
            return MyService.this;
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate: ");
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG, "onBind: ");
        return mBinder;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.d(TAG, "onUnbind: ");
        return super.onUnbind(intent);
    }

    // 바인드된 컴포넌트에 카운팅 변수 값을 제공
    public int getCount() {
        return mCount;
    }

    private void startForegroundService() {
        // default 채널 ID로 알림 생성
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "default");
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentTitle("포그라운드 서비스");
        builder.setContentText("포그라운드 서비스 실행 중");
        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        builder.setContentIntent(pendingIntent);

        // 오레오에서는 알림 채널을 매니저에 생성해야 한다
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            manager.createNotificationChannel(new NotificationChannel("default", "기본 채널", NotificationManager.IMPORTANCE_DEFAULT));
        }

        // 포그라운드로 시작
        startForeground(1, builder.build());
    }
}

 

 

 


 

될 때까지 안드로이드 #25 [24장 알림과 알람매니저 – 1]

 

 

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="createNotification"
        android:text="알림 생성" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="removeNotification"
        android:text="알림 제거" />

</LinearLayout>

 

 

 

public class MainActivity extends AppCompatActivity {

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

    private void show() {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "default");

        // 필수 항목
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentTitle("알림 제목");
        builder.setContentText("알림 세부 텍스트");

        // 액션 정의
        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,
                0,
                intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        // 클릭 이벤트 설정
        builder.setContentIntent(pendingIntent);

        // 큰 아이콘 설정
        Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
        builder.setLargeIcon(largeIcon);

        // 색상 변경
        builder.setColor(Color.RED);

        // 기본 알림음 사운드 설정
        Uri ringtoneUri = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_NOTIFICATION);
        builder.setSound(ringtoneUri);

        // 진동설정: 대기시간, 진동시간, 대기시간, 진동시간 ... 반복 패턴
        long[] vibrate = {0, 100, 200, 300};
        builder.setVibrate(vibrate);
        builder.setAutoCancel(true);

        // 알림 매니저
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        // 오레오에서는 알림 채널을 매니저에 생성해야 한다
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("default", "기본 채널", NotificationManager.IMPORTANCE_DEFAULT);
            channel.enableVibration(true);
            manager.createNotificationChannel(channel);
        }

        // 알림 통지
        manager.notify(1, builder.build());
    }

    private void hide() {
        // 알림 해제
        NotificationManagerCompat.from(this).cancel(1);
    }

    public void createNotification(View view) {
        show();
    }

    public void removeNotification(View view) {
        hide();
    }
}

 

 

 


 

될 때까지 안드로이드 #26 [24장 알림과 알람매니저 – 2]

 

 

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="showAlarmDialog"
        android:text="알람 설정" />

</LinearLayout>

 

 

 

public class MainActivity extends AppCompatActivity {

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

    public void showAlarmDialog(View view) {
        TimePickerFragment timePickerFragment = new TimePickerFragment();
        timePickerFragment.show(getSupportFragmentManager(), "timePicker");
    }
}

 

 

 

public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {

    private AlarmManager mAlarmManager;

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // 알람 매니저 인스턴스 얻기
        mAlarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);

        // 현재 시간으로 타임 피커를 설정
        final Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);

        // 타임 피커 다이얼로그를 현재 시간 설정으로 생성하고 반환
        return new TimePickerDialog(getContext(), this, hour, minute,
                DateFormat.is24HourFormat(getContext()));
    }

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        // 설정된 시간
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
        calendar.set(Calendar.MINUTE, minute);

        // 알람이 동작되면 MainActivity를 실행하도록 동작 정의
        // 여기서 브로드캐스트나 서비스를 실행할 수도 있음
        Intent intent = new Intent(getContext(), MainActivity.class);
        PendingIntent operation = PendingIntent.getActivity(getContext(), 0, intent, 0);

        // 설정된 시간에 기기가 슬립상태에서도 알람이 동작되도록 설정
        mAlarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), operation);
    }
}

 

 

 


 

될 때까지 안드로이드 #27 [25장 지도를 이용해보자1 – 구글 지도]

 

  1. 액티비티를 만들 때 구글맵 액티비티 템플릿을 사용한다.
  2. google_maps_api.xml 에 있는 링크를 따라가서 API 키를 가져온다.

 

implementation 'com.google.android.gms:play-services-maps:17.0.0'
implementation 'com.google.android.gms:play-services-location:17.0.0'

 

 

 

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.mapexam.MapsActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onLastLocationButtonClicked"
        android:text="현재 위치" />

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</FrameLayout>

 

 

 

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

    // 권한 체크 요청 코드 정의
    public static final int REQUEST_CODE_PERMISSIONS = 1000;

    private GoogleMap mMap;
    private GoogleApiClient mGoogleApiClient;

    // 위치 정보 얻는 객체
    private FusedLocationProviderClient mFusedLocationClient;

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

        // GoogleAPIClient의 인스턴스 생성
        if (mGoogleApiClient == null) {
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .addApi(LocationServices.API)
                    .build();
        }

        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
    }

    @Override
    protected void onStart() {
        mGoogleApiClient.connect();
        super.onStart();
    }

    @Override
    protected void onStop() {
        mGoogleApiClient.disconnect();
        super.onStop();
    }

    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));

        // 새로운 위치 추가
        LatLng gwangjuyeok = new LatLng(35.165352, 126.909222);
        mMap.addMarker(new MarkerOptions()
                .position(gwangjuyeok)
                .title("광주 광주역"));

        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(gwangjuyeok, 17.0f));

        // 카메라 줌
        //mMap.animateCamera(CameraUpdateFactory.zoomTo(17.0f));

        // 인포 윈도우 클릭시 전화 걸기
        mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {
                Intent intent = new Intent(Intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:06212345678"));
                if (intent.resolveActivity(getPackageManager()) != null) {
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        switch (requestCode) {
            case REQUEST_CODE_PERMISSIONS:
                if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(this, "권한 체크 거부 됨", Toast.LENGTH_SHORT).show();
                }
                return;
        }

    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

    }

    public void onLastLocationButtonClicked(View view) {
        // 권한 체크
        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_CODE_PERMISSIONS);
            return;
        }
        mFusedLocationClient.getLastLocation().addOnSuccessListener(this, new OnSuccessListener<Location>() {
            @Override
            public void onSuccess(Location location) {
                if (location != null) {
                    // 현재 위치
                    LatLng myLocation = new LatLng(location.getLatitude(), location.getLongitude());
                    mMap.addMarker(new MarkerOptions()
                            .position(myLocation)
                            .title("현재 위치"));

                    mMap.moveCamera(CameraUpdateFactory.newLatLng(myLocation));

                    // 카메라 줌
                    mMap.animateCamera(CameraUpdateFactory.zoomTo(17.0f));
                }
            }
        });
    }
}

 

 

 


 

될 때까지 안드로이드 (오준석의 생존코딩) 유튜브 강의
https://www.youtube.com/watch?v=euTtMnN-TgI&list=PLxTmPHxRH3VWTd-8KB67Itegihkl4SVKe&index=2

 

될 때까지 안드로이드 (오준석의 생존코딩) 깃헙
https://github.com/junsuk5/android-first-book

 

될 때까지 안드로이드 소스 다운로드
android-first-book-master.zip

 

 

 

Related posts

Leave a Comment