版权声明:本文为博主原创文章,未经博主允许不得转载。
https://blog.csdn.net/weixin_40790006/article/details/79934324
服务:实现通知栏
package com.example.f405.servicetest.utils; import android.app.Notification; import android.app.PendingIntent; import android.app.Service; import android.content.Intent; import android.graphics.BitmapFactory; import android.os.Binder; import android.os.IBinder; import android.support.annotation.Nullable; import android.support.v4.app.NotificationCompat; import android.util.Log; import android.widget.Toast; import com.example.f405.servicetest.Main2Activity; import com.example.f405.servicetest.MainActivity; import com.example.f405.servicetest.R; import static com.example.f405.servicetest.R.mipmap.ic_launcher_round; /** * Created by F405 on 2018/4/13. */ // 四大组件,清单文件注册 public class MyService extends Service { private static final String TAG = "MyService"; private MyIBinder myIBinder = new MyIBinder(); // 通信数据传输;由里面的方法实现 public class MyIBinder extends Binder{ public void getBinderData(){ Toast.makeText(MyService.this, "get", Toast.LENGTH_SHORT).show(); } public void getBinderData2(){ Toast.makeText(MyService.this, "get2", Toast.LENGTH_SHORT).show(); } } @Override public void onCreate() { super.onCreate(); Log.d(TAG, "onCreate: ----"); Intent intent = new Intent(this, Main2Activity.class); PendingIntent pendingIntent = PendingIntent.getActivities(this,0, new Intent[]{intent},0); Notification notification = new NotificationCompat.Builder(this) .setContentTitle("title") .setContentText("content") .setWhen(System.currentTimeMillis()) .setSmallIcon(R.mipmap.ic_launcher) .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher_round)) .setContentIntent(pendingIntent) .build(); startForeground(1,notification); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d(TAG, "onStartCommand: ----"); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { super.onDestroy(); Log.d(TAG, "onDestroy: ----"); } // 与活动通信 @Nullable @Override public IBinder onBind(Intent intent) { Log.d(TAG, "onBind: ----"); return myIBinder; } }
Main:
package com.example.f405.servicetest; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.os.IBinder; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import com.example.f405.servicetest.utils.MyService; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Button btn_service; private Button btn_bindService; private Intent intent; MyService.MyIBinder myIBinder; private static final String TAG = "MainActivity"; ServiceConnection connec = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { myIBinder = (MyService.MyIBinder) service; // 活动里调用/连接服务 myIBinder.getBinderData(); myIBinder.getBinderData2(); } @Override public void onServiceDisconnected(ComponentName name) { Log.d(TAG, "onServiceDisconnected: ----"); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { btn_service = (Button) findViewById(R.id.btn_service); btn_bindService = (Button) findViewById(R.id.btn_bindService); btn_service.setOnClickListener(this); btn_bindService.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.btn_service: intent = new Intent(this, MyService.class); startService(intent); break; case R.id.btn_bindService: Intent bind = new Intent(this,MyService.class); bindService(bind,connec,BIND_AUTO_CREATE); break; } } @Override protected void onDestroy() { super.onDestroy(); // 判断,没有注册,就不取消注册 if (connec != null){ unbindService(connec); } if (intent != null){ stopService(intent); } } }