4
回答
android开发的service问题
package com.example.servicedemo2;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class ExampleServer extends Service {
	
	private static final String TAG="ExampleServer";
	
	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}

	@Override
	public void onCreate() {
		Log.i(TAG, "ExampleServer-->onCreate");
		super.onCreate();
	}

	@Override
	public void onDestroy() {
		Log.i(TAG, "ExampleServer-->onDestroy");
		super.onDestroy();
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		Log.i(TAG, "ExampleServer-->onStartCommand");
		return super.onStartCommand(intent, flags, startId);
	}
	

}

package com.example.servicedemo2;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.support.v4.app.NavUtils;

public class MainActivity extends Activity {
	private Button btnStartServiceButton;
	private Button btnStopServiceButton;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnStartServiceButton = (Button) findViewById(R.id.btnStartService);
        btnStopServiceButton = (Button) findViewById(R.id.btnStopService);
        btnStartServiceButton.setOnClickListener(listener);
        btnStopServiceButton.setOnClickListener(listener);
    }
    
    private OnClickListener listener = new OnClickListener() {
    	Intent intent=new Intent(MainActivity.this, ExampleServer.class);
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btnStartService:
				
				startService(intent);
				break;
			case R.id.btnStopService:
				stopService(intent);
				break;
			default:
				break;
			}
			
		}
	};
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    
}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    >

    <Button
        android:id="@+id/btnStartService"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Start Service"
        />
    <Button
        android:id="@+id/btnStopService"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Stop Service"
        />

</LinearLayout>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.servicedemo2"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".ExampleService" />
    </application>

</manifest>

每次在安卓模拟器上运行没看见界面就显示已经停止运行,我看着视频已经对了几次了,没发现错误啊.求指教

举报
-Jw-
发帖于6年前 4回/887阅
顶部