السلام عليكم
تطبيق اندرويد اريد اضافة لنك تحميل الملفات عن طريق وضع رابط في انشاء مشاركة جديدة في لوحة الادمن وظهور زر التحميل في الاسفل مثل الصورة
هل يمكن مساعدتي او مشاركة الشرح
بعد إضافة الملفات في لوحة الأدمن، يتوجب للتطبيق أن يعمل استعلام من خلال موقع الويب ليعرف روابط هذه الملفات (أو إن كانت ثابتة تضيفها في التطبيق) أو جلب قائمة الروابط URL , من خلال API لوحة التحكم.
ثم في تطبيق أندرويد يتطلب إنشاء زر Button مثلاً أو أكثر و نربط الزر مع دالة لتحكم الملف
الزر:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/download_activity" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/download_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="198dp" android:text="Download" /> </RelativeLayout>
ثم عن طريق جافا، نربط الزر بالواجهة
public class MainActivity extends AppCompatActivity { private Button downloadButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.download_activity); downloadButton= (Button) findViewById(R.id.download_button); downloadButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // تجهيز الرابط Uri myURI_File = Uri.parse("عنوان الملف"); downloadfile(myURI_File ); } }); } // دالة تحميل الملف تستقبل رابط public void downloadFile(URI uri) { // مدير التحميل DownloadManager downloadManager = (DownloadManager) getActivity().getSystemService(Context.DOWNLOAD_SERVICE); // تهيئة الطلبية DownloadManager.Request request = new DownloadManager.Request(uri); request.setTitle("download file"); request.setDescription("download file description"); request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); Long reference = downloadManager.enqueue(request); } }
في حال أردت تحديد مكان حفظ الملف تعدل الدالة الخاصة بتحميل الملف
private File downloadFile(String url) throws ClientProtocolException, IOException { HttpGet httpGet = new HttpGet(url); DefaultHttpClient client = new DefaultHttpClient(); HttpResponse response = client.execute(httpGet); // تهيئة مكان التخزين File path = Environment.getExternalStorageDirectory(); File file = new File(path, "downloaded_file_" + System.currentTimeMillis()); // كتابة الملف byte[] bs = IOUtils.toByteArray(response.getEntity().getContent()); FileInputStream fileInputStream = new FileInputStream(file); fileInputStream.read(bs); fileInputStream.close(); return file; }
ما عرفت ربط الكود
هذا ملف MainActivity
package com.app.nestdevApkEditor.activities; import static com.app.nestdevApkEditor.utils.Constant.BANNER_HOME; import static com.app.nestdevApkEditor.utils.Constant.INTERSTITIAL_POST_LIST; import android.content.Intent; import android.content.IntentSender; import android.content.res.AssetManager; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.View; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.viewpager.widget.ViewPager; import com.app.nestdevApkEditor.BuildConfig; import com.app.nestdevApkEditor.R; import com.app.nestdevApkEditor.callbacks.CallbackUser; import com.app.nestdevApkEditor.config.AppConfig; import com.app.nestdevApkEditor.database.prefs.AdsPref; import com.app.nestdevApkEditor.database.prefs.SharedPref; import com.app.nestdevApkEditor.models.User; import com.app.nestdevApkEditor.rests.ApiInterface; import com.app.nestdevApkEditor.rests.RestAdapter; import com.app.nestdevApkEditor.utils.AdsManager; import com.app.nestdevApkEditor.utils.AppBarLayoutBehavior; import com.app.nestdevApkEditor.utils.Constant; import com.app.nestdevApkEditor.utils.RtlViewPager; import com.app.nestdevApkEditor.utils.Tools; import com.app.nestdevApkEditor.utils.ViewPagerHelper; import com.bumptech.glide.Glide; import com.bumptech.glide.load.engine.DiskCacheStrategy; import com.bumptech.glide.request.RequestOptions; import com.google.android.material.appbar.AppBarLayout; import com.google.android.material.bottomnavigation.BottomNavigationView; import com.google.android.material.bottomnavigation.LabelVisibilityMode; import com.google.android.material.snackbar.Snackbar; import com.google.android.play.core.appupdate.AppUpdateInfo; import com.google.android.play.core.appupdate.AppUpdateManager; import com.google.android.play.core.appupdate.AppUpdateManagerFactory; import com.google.android.play.core.install.model.AppUpdateType; import com.google.android.play.core.install.model.UpdateAvailability; import com.google.android.play.core.review.ReviewInfo; import com.google.android.play.core.review.ReviewManager; import com.google.android.play.core.review.ReviewManagerFactory; import com.google.android.play.core.tasks.Task; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; private long exitTime = 0; MyApplication myApplication; private BottomNavigationView navigation; private ViewPager viewPager; private RtlViewPager viewPagerRTL; TextView titleToolbar; User user; Call<CallbackUser> callbackCall = null; ImageView imgProfile; RelativeLayout btnProfile; ImageButton btnSearch; ImageButton btnSettings; SharedPref sharedPref; AdsPref adsPref; AdsManager adsManager; CoordinatorLayout parentView; ViewPagerHelper viewPagerHelper; private AppUpdateManager appUpdateManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Tools.getTheme(this); adsPref = new AdsPref(this); setContentView(R.layout.activity_main); Tools.setNavigation(this); sharedPref = new SharedPref(this); viewPagerHelper = new ViewPagerHelper(this); adsManager = new AdsManager(this); AppBarLayout appBarLayout = findViewById(R.id.tab_appbar_layout); ((CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams()).setBehavior(new AppBarLayoutBehavior()); myApplication = MyApplication.getInstance(); parentView = findViewById(R.id.tab_coordinator_layout); titleToolbar = findViewById(R.id.title_toolbar); imgProfile = findViewById(R.id.img_profile); navigation = findViewById(R.id.navigation); navigation.getMenu().clear(); if (sharedPref.getVideoMenu().equals("yes")) { navigation.inflateMenu(R.menu.menu_navigation_default); } else { navigation.inflateMenu(R.menu.menu_navigation_no_video); } navigation.setLabelVisibilityMode(LabelVisibilityMode.LABEL_VISIBILITY_LABELED); viewPager = findViewById(R.id.viewpager); viewPagerRTL = findViewById(R.id.viewpager_rtl); if (AppConfig.ENABLE_RTL_MODE) { viewPagerHelper.setupViewPagerRTL(viewPagerRTL, navigation, titleToolbar); } else { viewPagerHelper.setupViewPager(viewPager, navigation, titleToolbar); } Tools.notificationOpenHandler(this, getIntent()); initToolbarIcon(); displayUserProfile(); adsPref.saveCounter(1); adsManager.initializeAd(); adsManager.updateConsentStatus(); adsManager.loadBannerAd(BANNER_HOME); adsManager.loadInterstitialAd(INTERSTITIAL_POST_LIST, adsPref.getInterstitialAdInterval()); if (!BuildConfig.DEBUG) { appUpdateManager = AppUpdateManagerFactory.create(getApplicationContext()); inAppUpdate(); inAppReview(); } } public void showInterstitialAd() { adsManager.showInterstitialAd(); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } public void initToolbarIcon() { if (sharedPref.getIsDarkTheme()) { findViewById(R.id.toolbar).setBackgroundColor(getResources().getColor(R.color.colorToolbarDark)); navigation.setBackgroundColor(getResources().getColor(R.color.colorToolbarDark)); } else { findViewById(R.id.toolbar).setBackgroundColor(getResources().getColor(R.color.colorPrimary)); } btnSearch = findViewById(R.id.btn_search); btnSearch.setOnClickListener(view -> new Handler().postDelayed(() -> startActivity(new Intent(getApplicationContext(), ActivitySearch.class)), 50)); btnProfile = findViewById(R.id.btn_profile); btnProfile.setOnClickListener(view -> new Handler().postDelayed(() -> startActivity(new Intent(getApplicationContext(), ActivityProfile.class)), 50)); btnSettings = findViewById(R.id.btn_settings); btnSettings.setOnClickListener(view -> new Handler().postDelayed(() -> startActivity(new Intent(getApplicationContext(), ActivityProfile.class)), 50)); if (sharedPref.getLoginFeature().equals("yes")) { btnProfile.setVisibility(View.VISIBLE); btnSettings.setVisibility(View.GONE); } else { btnProfile.setVisibility(View.GONE); btnSettings.setVisibility(View.VISIBLE); } Tools.validate(this); } @Override public void onBackPressed() { if (AppConfig.ENABLE_RTL_MODE) { if (viewPagerRTL.getCurrentItem() != 0) { viewPagerRTL.setCurrentItem((0), true); } else { if (AppConfig.ENABLE_EXIT_DIALOG) { exitDialog(); } else { exitApp(); } } } else { if (viewPager.getCurrentItem() != 0) { viewPager.setCurrentItem((0), true); } else { if (AppConfig.ENABLE_EXIT_DIALOG) { exitDialog(); } else { exitApp(); } } } } public void exitApp() { if ((System.currentTimeMillis() - exitTime) > 2000) { showSnackBar(getString(R.string.press_again_to_exit)); exitTime = System.currentTimeMillis(); } else { finish(); } } public void showSnackBar(String msg) { Snackbar.make(parentView, msg, Snackbar.LENGTH_SHORT).show(); } public void exitDialog() { AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this); dialog.setTitle(R.string.dialog_close_title); dialog.setMessage(R.string.dialog_close_msg); dialog.setPositiveButton(R.string.dialog_option_quit, (dialogInterface, i) -> finish()); dialog.setNegativeButton(R.string.dialog_option_rate_us, (dialogInterface, i) -> { final String appName = getPackageName(); try { startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appName))); } catch (android.content.ActivityNotFoundException anfe) { startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + appName))); } finish(); }); dialog.setNeutralButton(R.string.dialog_option_more, (dialogInterface, i) -> { startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(sharedPref.getMoreAppsUrl()))); finish(); }); dialog.show(); } private void displayUserProfile() { if (myApplication.getIsLogin()) { requestUserData(); } else { imgProfile.setImageResource(R.drawable.ic_account_circle_white); } } private void requestUserData() { ApiInterface apiInterface = RestAdapter.createAPI(sharedPref.getBaseUrl()); callbackCall = apiInterface.getUser(myApplication.getUserId()); callbackCall.enqueue(new Callback<CallbackUser>() { @Override public void onResponse(Call<CallbackUser> call, Response<CallbackUser> response) { CallbackUser resp = response.body(); if (resp != null && resp.status.equals("ok")) { user = resp.response; if (user.image.equals("")) { imgProfile.setImageResource(R.drawable.ic_account_circle_white); } else { Glide.with(MainActivity.this) .load(sharedPref.getBaseUrl() + "/upload/avatar/" + user.image.replace(" ", "%20")) .apply(new RequestOptions().override(54, 54)) .diskCacheStrategy(DiskCacheStrategy.ALL) .centerCrop() .placeholder(R.drawable.ic_account_circle_white) .into(imgProfile); } } } @Override public void onFailure(Call<CallbackUser> call, Throwable t) { } }); } public void aboutDialog() { LayoutInflater layoutInflaterAndroid = LayoutInflater.from(MainActivity.this); View view = layoutInflaterAndroid.inflate(R.layout.custom_dialog_about, null); ((TextView) view.findViewById(R.id.txt_app_version)).setText(BuildConfig.VERSION_NAME + ""); final AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this); alert.setView(view); alert.setCancelable(false); alert.setPositiveButton(R.string.dialog_ok, (dialog, which) -> dialog.dismiss()); alert.show(); } @Override protected void onResume() { super.onResume(); displayUserProfile(); } @Override protected void onPause() { super.onPause(); } @Override public AssetManager getAssets() { return getResources().getAssets(); } private void inAppReview() { if (sharedPref.getInAppReviewToken() <= 3) { sharedPref.updateInAppReviewToken(sharedPref.getInAppReviewToken() + 1); } else { ReviewManager manager = ReviewManagerFactory.create(this); Task<ReviewInfo> request = manager.requestReviewFlow(); request.addOnCompleteListener(task -> { if (task.isSuccessful()) { ReviewInfo reviewInfo = task.getResult(); manager.launchReviewFlow(MainActivity.this, reviewInfo).addOnFailureListener(e -> { }).addOnCompleteListener(complete -> { Log.d(TAG, "In-App Review Success"); } ).addOnFailureListener(failure -> { Log.d(TAG, "In-App Review Rating Failed"); }); } }).addOnFailureListener(failure -> Log.d("In-App Review", "In-App Request Failed " + failure)); } Log.d(TAG, "in app review token : " + sharedPref.getInAppReviewToken()); } private void inAppUpdate() { Task<AppUpdateInfo> appUpdateInfoTask = appUpdateManager.getAppUpdateInfo(); appUpdateInfoTask.addOnSuccessListener(appUpdateInfo -> { if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) { startUpdateFlow(appUpdateInfo); } else if (appUpdateInfo.updateAvailability() == UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS) { startUpdateFlow(appUpdateInfo); } }); } private void startUpdateFlow(AppUpdateInfo appUpdateInfo) { try { appUpdateManager.startUpdateFlowForResult(appUpdateInfo, AppUpdateType.IMMEDIATE, this, Constant.IMMEDIATE_APP_UPDATE_REQ_CODE); } catch (IntentSender.SendIntentException e) { e.printStackTrace(); } } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == Constant.IMMEDIATE_APP_UPDATE_REQ_CODE) { if (resultCode == RESULT_CANCELED) { showSnackBar(getString(R.string.msg_cancel_update)); } else if (resultCode == RESULT_OK) { showSnackBar(getString(R.string.msg_success_update)); } else { showSnackBar(getString(R.string.msg_failed_update)); inAppUpdate(); } } } }
بصراحة الموضوع يحتاج خبرة برمجية جيدة في كل من BackEnd و Android من الصعب حل المشكلة بهذه الطريقة،ويتضمن والتجريب و التعديل في الكود حتى يعمل.
يمكنك تجريب حلول مع مشر أسئلة متعلقة بمشكلة محددة أثناء عملك للخطوات.
التعليقات