Explain.
앞서, intent기능을 활용한 social기능 구현하는법을 포스팅하였습니다.
이번에는 각 intent마다 필터링을 하여
각 app 마다 맞춰줘야할 템플릿을 맞춰주는것을 포스팅.
Code.
public void Share() { File videoFile = new File("My Video File Path"); Intent shareIntent = new Intent(Intent.ACTION_SEND); shareIntent.setType("video/mp4"); shareIntent.putExtra(Intent.EXTRA_SUBJECT, "My Video"); shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(videoFile)); shareIntent.putExtra(Intent.EXTRA_TEXT, "Enjoy the Video"); PackageManager pm = getPackageManager(); Intent sendIntent = new Intent(Intent.ACTION_SEND); sendIntent.setType("text/plain"); Intent openInChooser = Intent.createChooser(shareIntent, "select app to share"); // shareIntent를 기본으로한 chooser 생성. List<ResolveInfo> resInfo = pm.queryIntentActivities(shareIntent, 0); // share intent를 지원하는 resolveinfo 를 받아온다. List<LabeledIntent> intentList = new ArrayList<LabeledIntent>(); // 추가 intent를 담을 공간. for(ResolveInfo ri : resInfo) // 위에서 받아온 resolveinfo 반복(foreach) { String packageName = ri.activityInfo.packageName; if(packageName.contains("android.email")){ // 위에서 생성한 intent는 email 용이므로 intent에 email package를 넣어준다. shareIntent.setPackage(packageName); } else { // 그외의 의경우 Intent intent = new Intent(); intent.setComponent(new ComponentName(packageName, ri.activityInfo.name)); intent.setAction(Intent.ACTION_SEND); intent.setType("video/mp4"); if(packageName.contains("youtube")) // 현재 youtube filter적용 예시. { Log.d("SHARE", "In Youtube : " + packageName); ContentValues content = new ContentValues(4); content.put(Video.VideoColumns.TITLE, "My Video Title"); content.put(Video.VideoColumns.DATE_ADDED, System.currentTimeMillis() / 1000); content.put(Video.Media.MIME_TYPE, "video/mp4"); content.put(MediaStore.Video.Media.DATA, videoFile.getAbsolutePath()); ContentResolver resolver = getBaseContext().getContentResolver(); Uri uri = resolver.insert(MediaStore.Video.Media.INTERNAL_CONTENT_URI, content); // Extnernal 형식의 path의 경우 EXTERNAL_CONTENT_URI 사용 intent.setType("video/*"); intent.putExtra(Intent.EXTRA_STREAM, uri); } else // Youtube가 아닌 경우. { Log.d("SHARE", "Other : " + packageName); intent.putExtra(Intent.EXTRA_SUBJECT, "Video"); intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(videoFile)); intent.putExtra(Intent.EXTRA_TEXT, "Enjoy the Video"); } intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.icon)); } } LabeledIntent[] extraIntents = intentList.toArray( new LabeledIntent[ intentList.size() ]); openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents); startActivity(openInChooser); }
0 개의 댓글:
댓글 쓰기