Broadcast Receiver
Broadcast Receiver
AndroidManifest.xml
<receiver android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.TIMEZONE_CHANGED">
</action>
</intent-filter>
</receiver>
MainActivity.java
package com.example.broadcastexample;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
this.registerReceiver(myReceiver,intentFilter);
}
@Override
protected void onStop() {
super.onStop();
this.unregisterReceiver(myReceiver);
}
}
Myrreceiver.java
package com.example.broadcastexample;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/txtanim"
android:textSize="30dp"
android:textStyle="bold"
android:textColor="#E91E63"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:layout_marginTop="300dp"
android:orientation="horizontal">
<Button
android:id="@+id/translate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="translate"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alpha"
android:id="@+id/btnalpha"
/>
<Button
android:id="@+id/btnrotate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rotation"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="scale"
android:id="@+id/btnscale"
/>
</LinearLayout>
</LinearLayout>
MainActivity.java
package com.example.animationexample;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;
//moving
translate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Animation move=
AnimationUtils.loadAnimation(MainActivity.this,R.anim.move);
txtanim.startAnimation(move);
}
});
//visible to invisible
btnalpha.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Animation
alpha=AnimationUtils.loadAnimation(MainActivity.this,R.anim.alpha);
txtanim.startAnimation(alpha);
}
});
btnrotate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Animation
rotate=AnimationUtils.loadAnimation(MainActivity.this,R.anim.rotate);
txtanim.startAnimation(rotate);
}
});
btnscale.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Animation
scale=AnimationUtils.loadAnimation(MainActivity.this,R.anim.scale);
txtanim.startAnimation(scale);
}
});
}
}
<scale android:fromXScale="1"
android:toXScale="4"
android:fromYScale="1"
android:toYScale="4"
android:duration="4000"
android:pivotY="50%"
android:pivotX="50%"
/>
</set>
----------Rotate------------
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate android:fromDegrees="0"
android:toDegrees="360"
android:duration="12000"
android:pivotX="50%"
android:pivotY="50%"
/>
</set>
-------alpha-------------
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="0"
android:toAlpha="1"
android:duration="4000"
android:repeatCount="infinite"
/>
</set>
---------------translate-----------------
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="0"
android:toAlpha="1"
android:duration="4000"
android:repeatCount="infinite"
/>
</set>
package com.example.bluetooth;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Set;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = findViewById(R.id.bt1);
b2 = findViewById(R.id.bt2);
b3 = findViewById(R.id.bt3);
b4 = findViewById(R.id.bt4);
l1 = findViewById(R.id.lv);
BA= BluetoothAdapter.getDefaultAdapter();
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
on(v);
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
visible(v);
}
});
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
List(v);
}
});
b4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
off(v);
}
});
}
@SuppressLint("MissingPermission")
public void on(View v) {
if (!BA.isEnabled()) {
Intent turnON = new
Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnON, 0);
Toast.makeText(getApplicationContext(), "Turn on",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Already on",
Toast.LENGTH_SHORT).show();
}
}
@SuppressLint("MissingPermission")
public void off(View v) {
if (ActivityCompat.checkSelfPermission(this,
android.Manifest.permission.BLUETOOTH_CONNECT) !=
PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Permission not granted",
Toast.LENGTH_SHORT).show();
return;
}
if (BA.isEnabled()) {
BA.disable();
Toast.makeText(getApplicationContext(), "Bluetooth Turned Off",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Bluetooth is already off",
Toast.LENGTH_SHORT).show();
}
}
@SuppressLint("MissingPermission")
public void visible(View v)
{
Intent getVisible= new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(getVisible,0);
}
@SuppressLint("MissingPermission")
public void List(View v)
{
Set<BluetoothDevice> pairedDevices =BA.getBondedDevices();
ArrayList list=new ArrayList();
for(BluetoothDevice bt:pairedDevices)
list.add(bt.getName());
Toast.makeText(getApplicationContext(),"showing paired
device",Toast.LENGTH_LONG).show();
final ArrayAdapter adapter=new ArrayAdapter(this,
android.R.layout.simple_list_item_1,list);
l1.setAdapter(adapter);
XML------------------------------
<?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"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bluetooth"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textStyle="bold"
android:textSize="25dp"
android:textColor="#ffff0124"/>
<Button
android:text="TURN ON"
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:onClick="on"
/>
<Button
android:text="TURN VISIBLE"
android:id="@+id/bt2"
android:onClick="visible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
/>
<Button
android:text="LIST DEVICE"
android:id="@+id/bt3"
android:onClick="List"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
/>
<Button
android:text="TURN OFF"
android:id="@+id/bt4"
android:onClick="off"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Paired Device"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textStyle="bold"
android:textSize="25dp"
android:textColor="#ffff0124"
/>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/lv"
/>
</LinearLayout>
Permission--------------------------
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission
android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission
android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION"/>