Sunday, February 23, 2020

android study

터치 이벤트 처리


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if(event.getAction()==MotionEvent.ACTION_DOWN){
            initX=event.getRawX();

        }else if(event.getAction()==MotionEvent.ACTION_UP){
            float diffX=initX-event.getRawX();
            if(diffX>0){
                showToast("left");
            }else{
                showToast("right");
            }
        }
        return true;
    }

키 이벤트처리

//뒤로가기 버튼눌렀을때 "정말?" 처리하기
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK){
            if(System.currentTimeMillis()-initTime>3000){
                showToast("one more to finish");
                initTime=System.currentTimeMillis();
            }else{
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


Serializable Data 주고받기


//주기
intent.putExtra("news",((MyAdapter)mAdapter).getNews(position));

//받기
Intent intent=getIntent();
MyData mydata=(MyData)intent.getSerializableExtra("news");


Firebase Gradle설정(20.03.08) google-services.json 파일 추가되어야함

//App Level
apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.example.equal07.firebasetest"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.google.firebase:firebase-core:16.0.4'
    implementation 'com.android.support:support-v4:26.0.0'
    implementation 'com.google.firebase:firebase-ml-vision:18.0.1'
    // If you want to detect face contours (landmark detection and classification
    // don't require this additional model):
    implementation 'com.google.firebase:firebase-ml-vision-face-model:17.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
apply plugin: 'com.google.gms.google-services'


//Project Level
// Top-level build file where you can add configuration options common to all sub-projects/modules.

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
 
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.0'
        classpath 'com.google.gms:google-services:4.1.0'
     

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

findViewByID는 Activity 에서 사용가능하므로, 일반 클래스에서는 View를 얻어서 사용한다.

//아래와 같이 사용하기도 함.
public class MainActivity extends AppCompatActivity {
    Context mContext;
    protected void onCreate(Bundle savedInstanceState) {
        mContext=this;
    }
}


Display와 Image 사이즈를 맞취기위해선...

Point p = new Point();
Display display=getWindowManager().getDefaultDisplay();
display.getSize(p);

Source Level에서 View 그리기

ImageView imageLE=new ImageView(mContext);
imageLE.setImageResource(R.drawable.ms);
RelativeLayout_main.addView(imageLE);

//위치지정 p.x*lex/bitmap.getWidth()는 위치를 맞추기위한 로직, 상황에따라 수정
imageLE.setX(p.x*lex/bitmap.getWidth());
imageLE.setY(p.y*ley/bitmap.getHeight());

//사이즈 지정
imageLE.setLayoutParams(new RelativeLayout.LayoutParams(200,200));


RecyclerView에 Click Listener 추가하기


public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    private static View.OnClickListener onClickListener;
    private Context context;

    public static class MyViewHolder extends RecyclerView.ViewHolder{
        // each data item is just a string in this case
        // view declaration
        public View rootView;
        public MyViewHolder(View v) {
            super(v);
            //findViewByID BrBrBr...
            rootView = v;

            v.setClickable(true);
            v.setEnabled(true);
            v.setOnClickListener(onClickListener);
        }
    }

    // Provide a suitable constructor (depends on the kind of dataset)
    public MyAdapter(List<MyData> myDataset, Context context, View.OnClickListener onClick) {
        mDataset = myDataset;
        onClickListener=onClick;
        this.context=context;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
                                                     int viewType) {
        // create a new view
        View v = (View) LayoutInflater.from(parent.getContext())
                .inflate(R.layout.row, parent, false);

        MyViewHolder vh = new MyViewHolder(v);
        return vh;
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        // - get element from your dataset at this position
        // - replace the contents of the view with that element
        holder.textviewTitle.setText(mDataset.get(position).getTitle());
        holder.textviewContent.setText(mDataset.get(position).getContent());

        //Uri uri = Uri.parse(mDataset.get(position).getImgUrl());
        //holder.imageviewTitle.setImageURI(uri);
        Glide.with(context)
                .load(mDataset.get(position).getImgUrl())
                .into(holder.imageviewTitle);

        holder.rootView.setTag(position);

    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return mDataset.size();
    }

    public MyData getNews(int position){
        return mDataset!=null ? mDataset.get(position) : null;
    }
}


mAdapter = new MyAdapter(myDataset,MainActivity.this, new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Object obj=v.getTag();
                                    if(obj!=null){
                                        int position=(int)obj;
                                        //((MyAdapter)mAdapter).getNews(position);
                                        Intent intent = new Intent(MainActivity.this,DetailActivity.class);
                                        intent.putExtra("news",((MyAdapter)mAdapter).getNews(position));
                                        startActivity(intent);

                                    }
                                }
                            });


FileShareByIntent

//res/xml/*.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="myfiles" path="."/>
</paths>

//Manifaest
    </application>
        <provider
            android:authorities="com.example.part5_14.provider"  //Identifier with uniqueness
            android:name="android.support.v4.content.FileProvider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
        </provider>
    </application>

//FileShare
Uri phoneURI=FileProvider.getUriForFile(Lab14_2Activity.this, BuildConfig.APPLICATION_ID+
                        ".provider", filePath);
Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, phoneURI);
startActivityForResult(intent, 40);

FileWrite

String dirPath=Environment.getExternalStorageDirectory().getAbsolutePath()+"/myApp";//External Memory
        //String dirPath=Environment.getDataDirectory().getAbsolutePath()+"/myApp";//External Memory
        Log.d("yskTag","dirStr : "+dirPath);

        File dir=new File(dirPath);
        if(!dir.exists()){
            dir.mkdir();
        }

        File filePath;
        try {
            filePath=File.createTempFile("IMG", ".jpg", dir);

            Log.d("yskTag","fileTemp : "+filePath.toString());
            if(!filePath.exists()){
                filePath.createNewFile();
                Log.d("yskTag","fileNew : "+filePath.toString());
            }
            Log.d("yskTag",filePath.toString());
            Uri photoURI=FileProvider.getUriForFile(IntentGoogleMainActivity.this,BuildConfig.APPLICATION_ID+".provider",filePath);
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.ACTION_IMAGE_CAPTURE,photoURI);
            //sometimes "MediaStore.EXTRA_OUTPUT" works
            startActivityForResult(intent,10);
        } catch (IOException e) {
            e.printStackTrace();
            Log.d("yskTag","fileTemp : "+e.toString());
        }

Permission Granted

if(ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                ==PackageManager.PERMISSION_GRANTED){         
        }else {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    100);
        }


UTC to NormalTime

Date d = new java.util.Date(UTC*1000L);
String itemDateStr = new SimpleDateFormat("yyyy-MM-dd HH").format(d);
//UTC : second after 1970.01.01 00:00

Adding AdMob 

1. https://www.google.co.kr/ads/admob/ 에 가입후 로그인합니다.
2. 왼쪽 메뉴에서 앱을 클릭하고 보이는 창에서 앱 추가를 클릭합니다.
3. (앱을 게시하셨나요?)아니오를 선택합니다
4.  앱 이름을 입력하고  플랫폼에서 Android를 선택 후, 추가 버튼을 클릭합니다.
5. 앱 ID가 발급되었습니다.  다음 단계를 클릭합니다.
6. 앱에서 보여줄 광고 유형을 선택합니다. 본 포스트에서는 배너의 경우만 설명합니다. 배너에 있는 선택을 클릭합니다.
7. 광고 단위 이름을 적어주고 광고 단위 만들기를 클릭합니다.
8. 광고를 앱에 보여주기 위한 준비가 끝났습니다. 앱 ID와 광고 단위 ID를 따로 복사해두고 완료를 클릭합니다.
9. 이제 안드로이드 앱에 필요한 내용을 추가합니다.
strings.xml 파일에 앞에서 복사해두었던 앱 ID와 광고 단위 ID를 다음처럼 입력합니다.
banner_ad_unit_id_for_test는 광고가 제대로 보이는지 테스트하기 위해 사용합니다.
(예전과 달리)플레이스토어에 배포 된후에  광고가 보이기 때문이라고 합니다.

<resources>
    <string name="app_name">My Application</string>
    <string name="admob_app_id">앱 ID</string>
    <string name="banner_ad_unit_id">광고 단위 ID</string>
    <string name="banner_ad_unit_id_for_test">ca-app-pub-3940256099942544/6300978111</string>
</resources>


10. 매니페스트 파일에 필요한 권한을 추가합니다.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tistory.webnautes.myapplication">

    <!-- 인터넷을 사용하기 위한 권한입니다. -->
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <!-- play-services-ads 17.0.0 이상부터는 앱 ID를 입력해줘야 합니다 -->
        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="@string/admob_app_id"/>

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

11. build.gradle 파일에 애드몹을 사용하기 위해 필요한 패키지를 추가합니다.
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.google.android.gms:play-services-ads:18.0.0'

추가후 Sync Now를 해주면 다음과 같은 에러가 발생합니다. (근데 버전을 낮추면 에러가 안남? play-service랑 맞춰줘야해서 그런가?)

    implementation 'com.google.android.gms:play-services:12.0.1'
    implementation 'com.android.support:multidex:1.0.3'
    implementation 'com.google.android.gms:play-services-ads:12.0.1'


12. 레이아웃 파일에 애드몹을 보여주기 위한 코드를 추가합니다.
광고가 보이는지 테스트하기 위해 ads:adUnitId의 값으로 테스트용 광고 ID인  banner_ad_unit_id_for_test를 사용하고 있습니다.
플레이스토어에 배포할 때에는  banner_ad_unit_id_for_test 대신에 banner_ad_unit_id를 사용하세요. 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/adView"
        android:text="Hello World!" />

    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_unit_id_for_test">
    </com.google.android.gms.ads.AdView>
</RelativeLayout>


코드 추가후 레이아웃 미리보기에서 광고가 다른 것에 가려지지 않는지 확인하세요.

13. 자바 코드에 애드몹 관련 코드를 추가합니다.
public class MainActivity extends AppCompatActivity {
    private AdView mAdView;

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

        MobileAds.initialize(this, getString(R.string.admob_app_id));

        mAdView = findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);

        // 광고가 제대로 로드 되는지 테스트 하기 위한 코드입니다.
        mAdView.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                // Code to be executed when an ad finishes loading.
    // 광고가 문제 없이 로드시 출력됩니다. 
                Log.d("@@@", "onAdLoaded");
            }

            @Override
            public void onAdFailedToLoad(int errorCode) {
                // Code to be executed when an ad request fails.
                // 광고 로드에 문제가 있을시 출력됩니다.
                Log.d("@@@", "onAdFailedToLoad " + errorCode);
            }


14. 다음처럼 테스트용 광고가 하단에 출력됩니다.