package com.sample;
// MainActivity.java
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
private Button btnStart;
private Button btnStop;
private EditText editShow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnStart = (Button)findViewById(R.id.startButton);
btnStop = (Button)findViewById(R.id.stopButton);
editShow = (EditText) findViewById(R.id.editShow);
btnStart.setOnClickListener(startListener);
btnStop.setOnClickListener(stopListener);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(".myBroadcastAction");
this.registerReceiver(new myBroadcastReciver(), intentFilter);
}
private OnClickListener startListener = new OnClickListener() {
public void onClick(View v) {
startService(new Intent(MainActivity.this, BackgroundService.class));
}
};
private OnClickListener stopListener = new OnClickListener() {
public void onClick(View v) {
stopService(new Intent(MainActivity.this,BackgroundService.class));
}
};
private class myBroadcastReciver extends BroadcastReceiver{
@Override
public void onReceive(Context arg0, Intent arg1) {
String str = "null";
if(arg1.getAction().equals(".myBroadcastAction")){
str = arg1.getStringExtra("data");
}
editShow.setText(str);
}
}
}
//service
package com.sample;
// BackgroundService.java
import java.io.IOException;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class BackgroundService extends Service {
private String sMsg = "";
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "Crete", Toast.LENGTH_SHORT).show();
System.out.println("Create");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId){
super.onStartCommand(intent, flags, startId);
Toast.makeText(this, "service Start", Toast.LENGTH_SHORT).show();
System.out.println("service start");
startThread();//啟動線程
return START_STICKY;
}
private void startThread() {
new Thread(){
public void run(){
Intent intent = new Intent();
int i = 0;
while(true){
try{
Thread.sleep(5000);
}catch(Exception e){
e.printStackTrace();
}
i++;
intent.setAction(".myBroadcastAction");
intent.putExtra("data", i+"");
sendBroadcast(intent);
}
}
}.start();
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "service stop", Toast.LENGTH_SHORT).show();
System.out.println("service stop");
}
}
//xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sample"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 廣播注冊 -->
<receiver android:name = ".broadcastReciver">
<intent-filter android:priority="20">
<action android:name=".myBroadcastAction"/>
</intent-filter>
</receiver>
<service android:name="com.sample.BackgroundService" />
</application>
</manifest>
錯誤信息:
<receiver android:name = ".broadcastReciver">
<intent-filter android:priority="20">
<action android:name=".myBroadcastAction"/>
</intent-filter>
</receiver>
<receiver android:name = ".myBroadcastReciver">
<intent-filter android:priority="20">
<action android:name=".myBroadcastAction"/>
</intent-filter>
</receiver>