Subject :- Mobile Application Development Subject Code :- 22617
Name :- Shivam Sainath Korpakwad Batch :- CO 6 IA Roll No. 24
Exercise :-
1). Write a program to start a wi-fi using service.
Code :-
ANDROID MANIFEST.XML
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.wifi">
<!--Put the permissions between the manifest and application opening tags-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<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>
ACTIVITY_MAIN.XML
<?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">
<!--Changes the state of Wi-Fi on button click-->
<Button
android:id="@+id/wifiSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Click" />
<!--Displays the state of Wi-Fi on button click-->
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/wifiSwitch"
android:layout_centerHorizontal="true"
android:hint="Wifi Status"
android:textSize="30sp" />
</RelativeLayout>
MAINACTIVITY.KT
import android.net.wifi.WifiManager
import android.os.Build
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity()
{
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val btn = findViewById<Button>(R.id.wifiSwitch)
val textView = findViewById<TextView>(R.id.tv)
val wifi = applicationContext.getSystemService(WIFI_SERVICE) as WifiManager
btn.setOnClickListener
{
wifi.isWifiEnabled = !wifi.isWifiEnabled
if (!wifi.isWifiEnabled)
{
textView.text = "Wifi is ON"
}
else
{
textView.text = "Wifi is OFF"
}
}
}
}
Output :-
2). Write a program to display the following output.
Code :-
package com.example.create_stop_service;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button start = (Button) findViewById(R.id.startService);
Button stop = (Button) findViewById(R.id.stopservice);
start.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
startService(new Intent(MainActivity.this, MyService.class));
}
});
stop.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
stopService(new Intent(MainActivity.this, MyService.class));
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
MYSERVICE.JAVA
package com.example.create_stop_service;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class MyService extends Service
{
@Override
public IBinder onBind(Intent intent)
{
return null;
}
@Override
public void onCreate()
{
Toast.makeText(this, " MyService Created ", Toast.LENGTH_LONG).show();
}
@Override
public void onStart(Intent intent, int startId)
{
Toast.makeText(this, " MyService Started", Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy()
{
Toast.makeText(this, "MyService Stopped", Toast.LENGTH_LONG).show();
}
}
ACTIVITY_MAIN.XML
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/startService"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="90dp"
android:text="Start Service" />
<Button
android:id="@+id/stopservice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/startService"
android:layout_alignParentRight="true"
android:layout_below="@+id/startService"
android:layout_marginTop="86dp"
android:text="Stop Service" />
</RelativeLayout>
ANDROIDMANIFEST.XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.vibratephone_service"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.vibratephone_service.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".VibrateService"/>
</application>
</manifest>
Output :-