Advertisement
slemos96

Untitled

Dec 4th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.53 KB | None | 0 0
  1. package com.example.backgroundappexample;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.view.Menu;
  7. import android.view.View;
  8. import android.view.View.OnClickListener;
  9. import android.widget.Button;
  10. import android.widget.TextView;
  11.  
  12. public class BackgroundAppExample extends Activity {
  13.  
  14. public static boolean isService = false;
  15. @Override
  16. public void onCreate(Bundle savedInstanceState) {
  17.     super.onCreate(savedInstanceState);
  18.     setContentView(R.layout.main);
  19.  
  20.     Button startserviceButton = (Button) findViewById(R.id.button1);
  21.     startserviceButton.setOnClickListener(new OnClickListener() {
  22.         @Override
  23.         public void onClick(View v) {
  24.             startService(new Intent(BackgroundAppExample.this,BackgroundService.class));
  25.             Intent startMain = new Intent(Intent.ACTION_MAIN);
  26.             startMain.addCategory(Intent.CATEGORY_HOME);
  27.             startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  28.             startActivity(startMain);
  29.             isService = true;
  30.         }
  31.     });
  32. }
  33.  
  34. @Override
  35. protected void onResume() {
  36.     super.onResume();
  37.     stopService(new Intent(BackgroundAppExample.this,
  38.             BackgroundService.class));
  39.     if(isService)
  40.     {
  41.         TextView tv = (TextView) findViewById(R.id.textView1);
  42.         tv.setText("Service Resumed");
  43.         isService = false;
  44.     }
  45. }
  46. @Override
  47. public boolean onCreateOptionsMenu(Menu menu) {
  48.     getMenuInflater().inflate(R.menu.main, menu);
  49.     return true;
  50.  }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement