Service獲取隨機值、獲取本地時間
阿新 • • 發佈:2019-01-12
MainActivity
package com.example.demo01_service; 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.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import com.example.demo01_service.service.Service; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Service.Mybind bind; ServiceConnection connection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { bind = (Service.Mybind) iBinder; } @Override public void onServiceDisconnected(ComponentName componentName) { } }; private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //跳轉 Intent intent = new Intent(MainActivity.this,Service.class); //開啟service //註冊service bindService(intent,connection,Service.BIND_AUTO_CREATE); //獲取控制元件 Button button = findViewById(R.id.butt_dj); textView = findViewById(R.id.textview); button.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.butt_dj: //獲取本地時間 String date = bind.getDate(); textView.setText(""+date); //吐絲隨機數 Toast.makeText(MainActivity.this,""+bind.getRandom(),Toast.LENGTH_SHORT).show(); break; } } }
Service
package com.example.demo01_service.service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.support.annotation.Nullable; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Random; /** * @Auther: 努力 * @Date: 2019/1/2 10:43:${盧文傑} * @Description: */ public class Service extends android.app.Service { //內部類 public class Mybind extends Binder{ //隨機值 public String getRandom() { Random random = new Random(); return random.nextInt(9) + ""; } // 獲取本地時間 public String getDate() { SimpleDateFormat simpleDateFormat = new SimpleDateFormat( "yyyy年MM月dd日 HH:mm:ss"); Date date = new Date(System.currentTimeMillis()); String timeDate = simpleDateFormat.format(date); return timeDate; } } //例項化 private Mybind mybind = new Mybind(); @Nullable @Override public IBinder onBind(Intent intent) { return mybind; } }
切記一定要在清單檔案中註冊
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.demo01_service"> <application 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=".service.Service"/> </application> </manifest>