[embedyt]https://www.youtube.com/watch?v=Oenqq3ZCo9Q[/embedyt]
App.javaactivity_main.xmlExampleIntentService.javaMainActivity.javaAndroidManifest.xml
package com.codinginflow.intentserviceexample; import android.app.Application; import android.app.NotificationChannel; import android.app.NotificationManager; import android.os.Build; public class App extends Application { public static final String CHANNEL_ID = "exampleServiceChannel"; @Override public void onCreate() { super.onCreate(); createNotificationChannel(); } private void createNotificationChannel() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel serviceChannel = new NotificationChannel( CHANNEL_ID, "Example Service Channel", NotificationManager.IMPORTANCE_DEFAULT ); NotificationManager manager = getSystemService(NotificationManager.class); manager.createNotificationChannel(serviceChannel); } } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:gravity="center" android:orientation="vertical" android:padding="16dp" tools:context=".MainActivity"> <EditText android:id="@+id/edit_text_input" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Input" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="startService" android:text="Start Service" /> </LinearLayout>
package com.codinginflow.intentserviceexample; import android.app.IntentService; import android.app.Notification; import android.content.Intent; import android.os.Build; import android.os.PowerManager; import android.os.SystemClock; import android.support.annotation.Nullable; import android.support.v4.app.NotificationCompat; import android.util.Log; import static com.codinginflow.intentserviceexample.App.CHANNEL_ID; public class ExampleIntentService extends IntentService { private static final String TAG = "ExampleIntentService"; private PowerManager.WakeLock wakeLock; public ExampleIntentService() { super("ExampleIntentService"); setIntentRedelivery(true); } @Override public void onCreate() { super.onCreate(); Log.d(TAG, "onCreate"); PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE); wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ExampleApp:Wakelock"); wakeLock.acquire(); Log.d(TAG, "Wakelock acquired"); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID) .setContentTitle("Example IntentService") .setContentText("Running...") .setSmallIcon(R.drawable.ic_android) .build(); startForeground(1, notification); } } @Override protected void onHandleIntent(@Nullable Intent intent) { Log.d(TAG, "onHandleIntent"); String input = intent.getStringExtra("inputExtra"); for (int i = 0; i < 10; i++) { Log.d(TAG, input + " - " + i); SystemClock.sleep(1000); } } @Override public void onDestroy() { super.onDestroy(); Log.d(TAG, "onDestroy"); wakeLock.release(); Log.d(TAG, "Wakelock released"); } }
package com.codinginflow.intentserviceexample; import android.content.Intent; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; public class MainActivity extends AppCompatActivity { private EditText editTextInput; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editTextInput = findViewById(R.id.edit_text_input); } public void startService(View v) { String input = editTextInput.getText().toString(); Intent serviceIntent = new Intent(this, ExampleIntentService.class); serviceIntent.putExtra("inputExtra", input); ContextCompat.startForegroundService(this, serviceIntent); } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.codinginflow.intentserviceexample"> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> <application android:name=".App" 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"> <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=".ExampleIntentService" /> </application> </manifest>