Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MainActivity.java
- ==================
- package com.repoai.mycustomservices;
- import androidx.appcompat.app.AppCompatActivity;
- import android.content.Context;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- public class MainActivity extends AppCompatActivity {
- Context context;
- Intent myServiceClient;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- setPointer();
- }
- private void setPointer() {
- this.context=this;
- myServiceClient = new Intent(context,MyService.class);
- findViewById(R.id.btnStart).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- //start service
- startService(myServiceClient);
- }
- });
- findViewById(R.id.btnStop).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- //stop service
- stopService(myServiceClient);
- }
- });
- }
- }
- activity_main.xml
- =====================
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:gravity="center"
- android:background="#000">
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Start me"
- android:background="#00ff00"
- android:textColor="#ff0000"
- android:textSize="32sp"
- android:id="@+id/btnStart"
- />
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Stop me"
- android:background="#ff0000"
- android:textColor="#00ff00"
- android:textSize="32sp"
- android:layout_marginTop="32dp"
- android:id="@+id/btnStop"
- />
- </LinearLayout>
- MyService.java
- ==================
- package com.repoai.mycustomservices;
- import android.app.Service;
- import android.content.Intent;
- import android.os.IBinder;
- import android.widget.Toast;
- import androidx.annotation.Nullable;
- public class MyService extends Service {
- @Nullable
- @Override
- public IBinder onBind(Intent intent) {
- return null;
- }
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- Toast.makeText(this, "Gal hav a mac, soon comes the bucks!!!", Toast.LENGTH_SHORT).show();
- return super.onStartCommand(intent, flags, startId);
- }
- @Override
- public void onDestroy() {
- Toast.makeText(this, "Kaput !!!", Toast.LENGTH_LONG).show();
- super.onDestroy();
- }
- }
- AndroidMenifest.xml
- =====================
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- package="com.repoai.mycustomservices">
- <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"
- tools:ignore="GoogleAppIndexingWarning">
- <activity android:name=".MainActivity">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <service android:name=".MyService"/>
- </application>
- </manifest>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement