Android Service Notification Dymanic
JAVA:MyService
package com.example.joe.androidservicedemo;
import android.app.Notification;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.Process;
import android.support.v4.app.NotificationCompat;
import android.widget.RemoteViews;
import android.widget.Toast;
/**
* Created by joe on 2017/8/30.
*/
public class MyService extends Service {
private Looper mServiceLooper;
private ServiceHandler mServiceHandler;
Notification notification;
RemoteViews remoteViews;
// Handler that receives messages from the thread
private class ServiceHandler extends Handler{
public ServiceHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
int i = 0;
while (i < 10) {
try {
remoteViews.setTextViewText(R.id.textView_value,String.valueOf(i));
notification = new NotificationCompat.Builder(getApplicationContext())
.setContentTitle("Service Notification")
.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(true)
.setContent(remoteViews)
.build();
startForeground(msg.arg1,notification);
Thread.sleep(1000);
i ++;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
stopForeground(true);
stopSelf(msg.arg1);
}
}
@Override
public void onCreate() {
// Start up the thread running the service. Note that we create a
// separate thread because the service normally runs in the process's
// main thread, which we don't want to block. We also make it
// background priority so CPU-intensive work will not disrupt our UI.
HandlerThread thread = new HandlerThread("ServiceStartArguments",
Process.THREAD_PRIORITY_BACKGROUND);
thread.start();
// Get the HandlerThread's Looper and use it for our Handler
mServiceLooper = thread.getLooper();
mServiceHandler = new ServiceHandler(mServiceLooper);
remoteViews = new RemoteViews(this.getPackageName(),R.layout.remote_layout);
remoteViews.setTextViewText(R.id.textView_value,String.valueOf(0));
notification = new NotificationCompat.Builder(getApplicationContext())
.setContentTitle("Service Notification")
.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(true)
.setContent(remoteViews)
.build();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
// For each start request, send a message to start a job and deliver the
// start ID so we know which request we're stopping when we finish the job
Message msg = mServiceHandler.obtainMessage();
msg.arg1 = startId;
startForeground(msg.arg1,notification);
mServiceHandler.sendMessage(msg);
// If we get killed, after returning from here, restart
return START_REDELIVER_INTENT;
}
@Override
public IBinder onBind(Intent intent) {
// We don't provide binding, so return null
return null;
}
@Override
public void onDestroy() {
Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show();
}
}
JAVA MainActivity
package com.example.joe.androidservicedemo;
import android.app.Service;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this,MyService.class);
startService(intent);
}
}
XML activity_main
<?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="com.example.joe.androidservicedemo.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
XML remote_layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal">
<TextView
android:id="@+id/textView"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Index" />
<TextView
android:id="@+id/textView_value"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center" />
</LinearLayout>