package com.hfm.countservice;
import android.app.Activity;
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.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import com.hfm.countservice.service.CountService;
import com.hfm.countservice.service.ICountService;
public class MainActivity extends Activity implements OnClickListener {
public static final String TAG = "MainActivity";
private TextView textView;
private Button startButton;
private Button stopButton;
private ICountService countService = null;
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
countService = null;
Log.i(TAG, "onServiceDisconnected");
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
countService = ((CountService.CounterBinder) service).getService();
Log.i(TAG, "onServiceConnected");
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textview_counter);
startButton = (Button) findViewById(R.id.start_button);
stopButton = (Button) findViewById(R.id.stop_button);
startButton.setOnClickListener(this);
stopButton.setOnClickListener(this);
startButton.setEnabled(true);
stopButton.setEnabled(false);
Intent bindIntent = new Intent(MainActivity.this, CountService.class);
Boolean boolean1 = bindService(bindIntent, serviceConnection, Context.BIND_AUTO_CREATE);
Log.i(TAG, "" + boolean1);
}
BroadcastReceiver counterReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
int val = intent.getIntExtra(CountService.COUNT_VALUE, 0);
textView.setText(Integer.toString(val));
}
};
protected void onResume() {
super.onResume();
IntentFilter intentFilter = new IntentFilter(CountService.BROADCAST_COUNTER_ACTION);
registerReceiver(counterReceiver, intentFilter);
}
protected void onStop(){
super.onResume();
unregisterReceiver(counterReceiver);
}
protected void onDestroy(){
super.onDestroy();
unbindService(serviceConnection);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.equals(startButton)) {
Log.i(TAG, "starButton is clicked");
if (countService != null) {
countService.startCount(0);
startButton.setEnabled(false);
stopButton.setEnabled(true);
}
}
if(v.equals(stopButton)){
Log.i(TAG, "stopButton is clicked");
if(countService != null){
countService.stopCount();
startButton.setEnabled(true);
stopButton.setEnabled(false);
}
}
}
}]
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.hfm.countservice" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <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> <service android:name=".CountService" android:enabled="true"> </service> </application> </manifest> |
|
|
原来是manifest文件注册Service时,Service所在的包名写错了,因为主Activity和Service不在同一个包中,
|
|
20分 |
除了名字,包名有时候也容易错,在eclipse里写全包名然后用alt+/提示出来也是个防错方法 |
|
谢谢~ |
|