Skip to content

Instantly share code, notes, and snippets.

View Shivamdhuria's full-sized avatar

Shivam Dhuria Shivamdhuria

View GitHub Profile
import static elixer.com.foregroundservice.App.CHANNEL_ID;
public class ForegroundService extends Service {
MediaPlayer myPlayer;
@Override
public void onCreate() {
super.onCreate();
myPlayer = MediaPlayer.create(this, R.raw.track);
myPlayer.setLooping(false); // Set looping
<?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="elixer.com.foregroundservice">
<application
android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
@Shivamdhuria
Shivamdhuria / AndroidManifest.xml
Last active May 31, 2019 06:37
Bound Service example
<?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="elixer.com.boundservice">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="vertical">
<Button
android:id="@+id/bind"
android:layout_width="match_parent"
android:layout_height="wrap_content"
public class MainActivity extends AppCompatActivity {
//BoundService class Object
BoundService mBoundService;
boolean mServiceBound = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
public class BoundService extends Service {
private static String LOG_TAG = "BoundService";
private IBinder mBinder = new MyBinder();
private Chronometer mChronometer;
@Override
public void onCreate() {
super.onCreate();
Log.v(LOG_TAG, "in onCreate");
mChronometer = new Chronometer(this);
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
public class MyService extends IntentService {
public MyService() {
super("MyService");
}
@Override
protected void onHandleIntent(Intent intent) {
String message = intent.getStringExtra("message");
public class MainActivity extends AppCompatActivity {
TextView textView;
Button button;
EditText editText;
MyReceiver myReceiver;
public static final String FILTER_ACTION_KEY = "999";
@Override
<?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="elixer.com.intentservice">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"