Professional Documents
Culture Documents
Package Import Import Import Import Import Import Public Class Extends
Package Import Import Import Import Import Import Public Class Extends
android:layout_height="wrap_content" super.onCreate(savedInstanceState);
android:text="Wifi Turn On"
android:textStyle="bold" setContentView(R.layout.activity_main);
Button btntOn =
android:layout_marginLeft="50dp" (Button)findViewById(R.id.btnOn);
Button btntOff =
android:layout_marginTop="200dp" /> (Button)findViewById(R.id.btnOFF);
<Button btntOn.setOnClickListener(new
android:id="@+id/btnOFF" View.OnClickListener() {
@Override
android:layout_width="wrap_content" public void onClick(View v) {
WifiManager wmgr =
android:layout_height="wrap_content" (WifiManager)getApplicationContext().
android:layout_alignBottom= getSystemService(Context.WIFI_SERVICE);
"@+id/btnOn"
wmgr.setWifiEnabled(true);
android:layout_marginLeft="210dp" }
android:textStyle="bold" });
android:text="Wifi Turn OFF" btntOff.setOnClickListener(new
/> View.OnClickListener() {
</RelativeLayout> @Override
public void onClick(View v) {
WifiManager wmgr =
(WifiManager)getApplicationContext().
getSystemService(Context.WIFI_SERVICE);
wmgr.setWifiEnabled(false);
}
});
}
}
Androidmanifest.xml
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Output :
2. Write a program to display the following output.
activity_main.xml MainActivity.java
tools:context=".MainActivity"> @Override
public void onCreate(Bundle
savedInstanceState) {
<TextView super.onCreate(savedInstanceState);
android:id="@+id/textView2" setContentView
(R.layout.activity_main);
android:layout_width="wrap_content" Log.d(msg, "The onCreate()
event");
android:layout_height="wrap_content }
"
android:text="Tutorials point" public void startService
android:textColor="#ff87ff09" (View view) {
android:textSize="30dp" startService(new
Intent(getBaseContext(),
android:layout_centerHorizontal= MyService.class));
"true" }
android:layout_marginBottom=
"40dp" /> public void stopService
(View view)
{
<Button stopService(new
Intent(getBaseContext(),
android:layout_width="340dp" MyService.class));
}
android:layout_height="wrap_content }
"
android:id="@+id/button2"
android:text="Start Services"
android:onClick="startService"
android:layout_centerHorizontal=
"true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content
"
android:text="Stop Services"
android:id="@+id/button"
android:onClick="stopService"
android:layout_below="@+id/button2"
android:layout_alignLeft=
"@+id/button2"
android:layout_alignStart=
"@+id/button2"
android:layout_alignRight=
"@+id/button2"
android:layout_alignEnd=
"@+id/button2" />
</RelativeLayout>
@Override
public int onStartCommand(Intent
intent, int flags, int startId) {
HelloService.java
package com.example.mainactivity;
import android.app.Service;
import android.os.IBinder;
import android.content.Intent;
import android.os.Bundle;
int mStartMode;
IBinder mBinder;
boolean mAllowRebind;
@Override
public void onCreate() {
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
return mStartMode;
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
@Override
public boolean onUnbind(Intent intent) {
return mAllowRebind;
}
@Override
public void onRebind(Intent intent) {
@Override
public void onDestroy() {
}
}