download from link

how to download from link


main activity

public class MainActivity extends AppCompatActivity {
EditText mUrlEt; Button mDownloadBtn; private static final int PERMISSION_STORAGE_CODE=1000;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mUrlEt =findViewById(R.id.urlEt); mDownloadBtn=findViewById(R.id.downloadUrl);
mDownloadBtn.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
//if OS is Marshmallow or above handle runtime permission if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)== PackageManager.PERMISSION_DENIED){
//pending denied request it String[] permission ={Manifest.permission.WRITE_EXTERNAL_STORAGE}; //show popup for runtime permission requestPermissions(permission,PERMISSION_STORAGE_CODE); }else {
//permission already granted, perform download startDownloading();
}

}else {
//permission already granted, perform download startDownloading(); }
}
}); }
private void startDownloading(){
String url = mUrlEt.getText().toString().trim(); //create download request DownloadManager.Request request =new DownloadManager.Request(Uri.parse(url)); //allow types of network to download file request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI |
DownloadManager.Request.NETWORK_MOBILE); request.setTitle("Download");//set title in download notification request.setDescription("Downloading file...");//set description in download notification request.allowScanningByMediaScanner(); request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_ONLY_COMPLETION); request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,""+System.currentTimeMillis());//get current timestamp as file name
//get download service and enque file DownloadManager manager=(DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE); manager.enqueue(request);

}
//handel permission result
@Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode){
case PERMISSION_STORAGE_CODE:{
if(grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
startDownloading(); }else{
Toast.makeText(this,"Permission",Toast.LENGTH_SHORT).show();

}

}
}
}
}
XML file
<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:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity">
<!--EditText: Input Url --> <EditText android:id="@+id/urlEt" android:hint="Enter url.." android:layout_width="match_parent" android:layout_height="wrap_content"/>
<!--Button Download file from Url--> <Button android:id="@+id/downloadUrl" android:text="Download" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true"/>

</RelativeLayout>
Manifest file
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.para.download"><uses-permission android:name="android.permission.INTERNET"/>    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


<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"> <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>

Pdf in app

add this in build.gradel
implementation 'com.github.barteksc:android-pdf-viewer:3.0.0-beta.5'
go to new and add folder asset 
now paste pdf here
main activity
@Overrideprotected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //PDF View PDFView pdfView = findViewById(R.id.pdfView); pdfView.fromAsset("abc.pdf")
.enableSwipe(true) // allows to block changing pages using swipe .swipeHorizontal(false)
.enableDoubletap(true)
.defaultPage(0)
.enableAnnotationRendering(false) // render annotations (such as comments, colors or forms) .password(null)
.scrollHandle(null)
.enableAntialiasing(true) // improve rendering a little bit on low-res screens // spacing between pages in dp. To define spacing color, set view background .spacing(0)
.pageFitPolicy(FitPolicy.WIDTH)
.load();}
xml file
<?xml version="1.0" encoding="utf-8"?><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"    tools:context=".MainActivity">
<com.github.barteksc.pdfviewer.PDFView android:id="@+id/pdfView" android:layout_width="match_parent" android:layout_height="match_parent"/>
</LinearLayout>
open only when internet is on
main activity
public class MainActivity extends AppCompatActivity {

@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); android.net.NetworkInfo wifi = cm
.getNetworkInfo(ConnectivityManager.TYPE_WIFI); android.net.NetworkInfo datac = cm
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); if ((wifi != null & datac != null)
&& (wifi.isConnected() | datac.isConnected())) {
//connection is avlilable }else{
//no connection Toast toast = Toast.makeText(getApplicationContext(), "No Internet Connection", Toast.LENGTH_LONG); toast.show(); }
}
}
manifest file
<uses-permission android:name="android.permission.INTERNET"/><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

rewarded video advertisement
used this on dependencies
implementation 'com.google.android.gms:play-services-ads:19.2.0'
main activity
public class MainActivity extends AppCompatActivity implements RewardedVideoAdListener {

private RewardedVideoAd mAd;
private TextView mText;

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

mText=(TextView)findViewById(R.id.textView);

MobileAds.initialize(getApplicationContext(),"ca-app-pub-3940256099942544~3347511713");

mAd=MobileAds.getRewardedVideoAdInstance(this);
mAd.setRewardedVideoAdListener(this);
loadRewardedVideoAd();

}

private void loadRewardedVideoAd(){
if(!mAd.isLoaded()){
mAd.loadAd("ca-app-pub-3940256099942544/5224354917",new AdRequest.Builder().build() );
}
}

public void startVideo(View view) {
if(mAd.isLoaded()){
mAd.show();
}
}

@Override
public void onRewardedVideoAdLoaded() {

}

@Override
public void onRewardedVideoAdOpened() {

}

@Override
public void onRewardedVideoStarted() {

}

@Override
public void onRewardedVideoAdClosed() {

loadRewardedVideoAd();

}

@Override
public void onRewarded(RewardItem rewardItem) {
mText.setText("Available Coins :15");

}

@Override
public void onRewardedVideoAdLeftApplication() {

}

@Override
public void onRewardedVideoAdFailedToLoad(int i) {

}

@Override
public void onRewardedVideoCompleted() {

}
@Override
protected void onPause(){
mAd.pause(this);
super.onPause();
}

@Override
protected void onResume() {
mAd.resume(this);
super.onResume();
}

@Override
protected void onDestroy() {
mAd.destroy(this);
super.onDestroy();
}
}
public class MainActivity extends AppCompatActivity {
private final String CHANNEL_ID="personal_notification";
private final int NOTIFICATION_ID=001;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void displayNotification(View view){
creatNotificationChannel();

Intent landingIntent =new Intent(this,LandingActivity.class);
landingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);

PendingIntent landingPendingIntent =PendingIntent.getActivity(this,0,landingIntent,PendingIntent.FLAG_ONE_SHOT);

NotificationCompat.Builder builder= new NotificationCompat.Builder(this,CHANNEL_ID);
builder.setSmallIcon(R.drawable.ic_sms_notification);
builder.setContentTitle("simple Notification");
builder.setContentText("This is a simple notification");
builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
builder.setContentIntent(landingPendingIntent);
builder.setAutoCancel(true);
NotificationManagerCompat notificationManagerCompat=NotificationManagerCompat.from(this);
notificationManagerCompat.notify(NOTIFICATION_ID,builder.build());
}

private void creatNotificationChannel()
{
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O)
{
CharSequence name="Personal Notification";
String descriprtion="Include all the personal notification";
int importance= NotificationManager.IMPORTANCE_DEFAULT;

NotificationChannel notificationChannel= new NotificationChannel(CHANNEL_ID,name,importance);

notificationChannel.setDescription(descriprtion);
NotificationManager notificationManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(notificationChannel);
}
}

}
landing activity
public class LandingActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_landing);
}
}
xml file(main activity)
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">

<Button
android:id="@+id/button"
android:layout_width="125dp"
android:layout_height="61dp"
android:layout_marginTop="292dp"

android:onClick="displayNotification"
android:text="display notification"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
xml file(landing activity)
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".LandingActivity">

<TextView
android:id="@+id/textView"
android:layout_width="255dp"
android:layout_height="72dp"
android:layout_marginTop="276dp"
android:text="Landing activity"
android:textSize="35dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Comments

Popular posts from this blog

Parallel Universe Friend privacy policy

Privacy Policy for GandG: Geology and Geophysics

Falling Star privacy policy