main_activity

import androidx.appcompat.app.AppCompatActivity;import androidx.core.app.NotificationCompat;import android.app.Notification;import android.app.NotificationChannel;import android.app.NotificationManager;import android.app.PendingIntent;import android.app.ProgressDialog;import android.content.BroadcastReceiver;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.content.ServiceConnection;import android.graphics.Color;import android.media.RingtoneManager;import android.os.Build;import android.os.Bundle;import android.os.IBinder;import android.view.View;import android.widget.Button;import android.widget.RemoteViews;import android.widget.Toast;public class MainActivity extends AppCompatActivity {    public static final String TAG = "godv";    private static final String ACTION = "godv";    private static final int NOTIFICATION_ID = 9001;    private MsgService mSgService;    private ServiceConnection mConn;    private NotificationManager mManage;    private int mMark = 10;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Button mStarButton = findViewById(R.id.startTip);        Button mEndButton = findViewById(R.id.endTip);        connectService();        mStarButton.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                mSgService.startTip();            }        });        mEndButton.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                mSgService.endTip();            }        });        registerReceiver();    }    private void registerReceiver() {        BroadcastReceiver mReceiver = new NotificationBroadcast();        IntentFilter filter = new IntentFilter(ACTION);        registerReceiver(mReceiver, filter);    }    private void setListener() {        mSgService.setOnProgressListener(new OnProgressListener() {            @Override            public void onProgress(final int count) {                MainActivity.this.runOnUiThread(new Runnable() {                    @Override                    public void run() {                        showNotification(count + "");                        if (count % mMark == 0) {                            Toast.makeText(MainActivity.this, "godv", Toast.LENGTH_SHORT).show();                        }                    }                });            }        });    }    private void connectService() {        final ProgressDialog dialog = ProgressDialog.show(this, getResources().getString(R.string.tip),                getResources().getString(R.string.loading));        new Thread(new Runnable() {            @Override            public void run() {                mConn = new ServiceConnection() {                    @Override                    public void onServiceDisconnected(ComponentName name) {                    }                    @Override                    public void onServiceConnected(ComponentName name, IBinder service) {                        mSgService = ((MsgService.MsgBinder) service).getService();                        dialog.dismiss();                        runOnUiThread(new Runnable() {                            @Override                            public void run() {                                Toast.makeText(MainActivity.this, getResources().getString(R.string.success)                                        , Toast.LENGTH_SHORT).show();                            }                        });                        setListener();                    }                };                Intent intent = new Intent(MainActivity.this, MsgService.class);                bindService(intent, mConn, Context.BIND_AUTO_CREATE);            }        }).start();    }    @Override    protected void onDestroy() {        super.onDestroy();        unbindService(mConn);    }    public interface OnProgressListener {        void onProgress(int count);    }    private void showNotification(String context) {        String channelID = "channelID";        CharSequence channelName = "channelName";        mManage = (NotificationManager) getBaseContext().getSystemService(NOTIFICATION_SERVICE);        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {            NotificationChannel channel = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_HIGH);            channel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION),                    Notification.AUDIO_ATTRIBUTES_DEFAULT);            channel.enableLights(true);            channel.setLightColor(Color.RED);            mManage.createNotificationChannel(channel);        }        NotificationCompat.Builder builder = new NotificationCompat.Builder(getBaseContext(), channelID);        builder.setSmallIcon(R.mipmap.ic_launcher);        RemoteViews contentView = new RemoteViews(getBaseContext().getPackageName(), R.layout.notifa);        contentView.setTextViewText(R.id.notification_tv, context);        int requestCode = 1;        PendingIntent homeIntent = PendingIntent.getBroadcast(this, requestCode, new Intent(ACTION),                PendingIntent.FLAG_UPDATE_CURRENT);        contentView.setOnClickPendingIntent(R.id.notification_btn, homeIntent);        builder.setCustomContentView(contentView);        Notification notification = builder.build();        notification.flags = Notification.FLAG_ONGOING_EVENT;        mManage.notify(NOTIFICATION_ID, notification);    }    public class NotificationBroadcast extends BroadcastReceiver {        @Override        public void onReceive(Context context, Intent intent) {            mManage.cancel(NOTIFICATION_ID);            mSgService.endTip();        }    }}

service

import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;import android.os.SystemClock;import android.util.Log;/** * Implemented sevice class */public class MsgService extends Service {    //Intervals    private static final int INTERVAL = 1000;    //Simulate lazy loading    private static final int DELAY = 3000;    private static boolean sBoot = true;    private MainActivity.OnProgressListener mProgressListener;    private int mCount = 0;    private int mInit = 0;    public void setOnProgressListener(MainActivity.OnProgressListener onProgressListener) {        this.mProgressListener = onProgressListener;    }    public void startTip() {        sBoot = true;        new Thread(new Runnable() {            @Override            public void run() {                while (sBoot) {                    mCount++;                    Log.i(MainActivity.TAG, "mCount:" + mCount);                    mProgressListener.onProgress(mCount);                    SystemClock.sleep(INTERVAL);                }            }        }).start();    }    public void endTip() {        sBoot = false;        mCount = mInit;    }    @Override    public IBinder onBind(Intent intent) {        SystemClock.sleep(DELAY);        return new MsgBinder();    }    public class MsgBinder extends Binder {        public MsgService getService() {            return MsgService.this;        }    }}

配置service

Strings

    Service_Count    开始    停止    等待连接    提示    连接成功    退出

 

更多相关文章

  1. Android(安卓)bluetooth 开发
  2. 将获取的html源代码格式化输出
  3. Android(安卓)连接.net WebService 工具类代码
  4. Android判断网络是否连接
  5. ListView实现滚动动画
  6. Android(安卓)监控 网络连接状态
  7. Android(安卓)xml Activity进入或退出动画
  8. Android按返回键退出程序的两种方式
  9. android HttpClient连接Web端

随机推荐

  1. android 关于tts的一些参数
  2. 搭建android测试环境
  3. Android中通过Intent 调用图片、视频、音
  4. Android(安卓)EditText输入限制最大字符
  5. android 自定义控件属性
  6. Android——全屏显示的两种方式
  7. Android加速度传感器
  8. Android(安卓)IDL 小例子
  9. [置顶] Android(安卓)系列:安装Android(安
  10. Android解析XML