Advertisement
Guest User

services

a guest
Aug 21st, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.82 KB | None | 0 0
  1. MainActivity.java
  2. ==================
  3. package com.repoai.mycustomservices;
  4.  
  5. import androidx.appcompat.app.AppCompatActivity;
  6.  
  7. import android.content.Context;
  8. import android.content.Intent;
  9. import android.os.Bundle;
  10. import android.view.View;
  11.  
  12. public class MainActivity extends AppCompatActivity {
  13.     Context context;
  14.     Intent myServiceClient;
  15.  
  16.     @Override
  17.     protected void onCreate(Bundle savedInstanceState) {
  18.         super.onCreate(savedInstanceState);
  19.         setContentView(R.layout.activity_main);
  20.         setPointer();
  21.     }
  22.  
  23.     private void setPointer() {
  24.         this.context=this;
  25.         myServiceClient = new Intent(context,MyService.class);
  26.         findViewById(R.id.btnStart).setOnClickListener(new View.OnClickListener() {
  27.             @Override
  28.             public void onClick(View view) {
  29.                 //start service
  30.                 startService(myServiceClient);
  31.             }
  32.         });
  33.         findViewById(R.id.btnStop).setOnClickListener(new View.OnClickListener() {
  34.             @Override
  35.             public void onClick(View view) {
  36.                 //stop service
  37.                 stopService(myServiceClient);
  38.             }
  39.         });
  40.     }
  41. }
  42.  
  43.  
  44.  
  45.  
  46. activity_main.xml
  47. =====================
  48. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  49.     android:layout_width="match_parent"
  50.     android:layout_height="match_parent"
  51.     android:orientation="vertical"
  52.     android:gravity="center"
  53.     android:background="#000">
  54.  
  55.     <Button
  56.         android:layout_width="match_parent"
  57.         android:layout_height="wrap_content"
  58.         android:text="Start me"
  59.         android:background="#00ff00"
  60.         android:textColor="#ff0000"
  61.         android:textSize="32sp"
  62.         android:id="@+id/btnStart"
  63.         />
  64.  
  65.     <Button
  66.         android:layout_width="match_parent"
  67.         android:layout_height="wrap_content"
  68.         android:text="Stop me"
  69.         android:background="#ff0000"
  70.         android:textColor="#00ff00"
  71.         android:textSize="32sp"
  72.         android:layout_marginTop="32dp"
  73.         android:id="@+id/btnStop"
  74.         />
  75.  
  76.  
  77. </LinearLayout>
  78.  
  79.  
  80.  
  81. MyService.java
  82. ==================
  83. package com.repoai.mycustomservices;
  84.  
  85. import android.app.Service;
  86. import android.content.Intent;
  87. import android.os.IBinder;
  88. import android.widget.Toast;
  89.  
  90. import androidx.annotation.Nullable;
  91.  
  92. public class MyService extends Service {
  93.     @Nullable
  94.     @Override
  95.     public IBinder onBind(Intent intent) {
  96.         return null;
  97.     }
  98.  
  99.     @Override
  100.     public int onStartCommand(Intent intent, int flags, int startId) {
  101.         Toast.makeText(this, "Gal hav a mac, soon comes the bucks!!!", Toast.LENGTH_SHORT).show();
  102.         return super.onStartCommand(intent, flags, startId);
  103.     }
  104.  
  105.     @Override
  106.     public void onDestroy() {
  107.         Toast.makeText(this, "Kaput !!!", Toast.LENGTH_LONG).show();
  108.         super.onDestroy();
  109.     }
  110. }
  111.  
  112.  
  113.  
  114. AndroidMenifest.xml
  115. =====================
  116. <?xml version="1.0" encoding="utf-8"?>
  117. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  118.     xmlns:tools="http://schemas.android.com/tools"
  119.     package="com.repoai.mycustomservices">
  120.  
  121.     <application
  122.         android:allowBackup="true"
  123.         android:icon="@mipmap/ic_launcher"
  124.         android:label="@string/app_name"
  125.         android:roundIcon="@mipmap/ic_launcher_round"
  126.         android:supportsRtl="true"
  127.         android:theme="@style/AppTheme"
  128.         tools:ignore="GoogleAppIndexingWarning">
  129.         <activity android:name=".MainActivity">
  130.             <intent-filter>
  131.                 <action android:name="android.intent.action.MAIN" />
  132.  
  133.                 <category android:name="android.intent.category.LAUNCHER" />
  134.             </intent-filter>
  135.         </activity>
  136.        <service android:name=".MyService"/>
  137.     </application>
  138.  
  139. </manifest>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement