您的位置:首页 > 移动开发 > Android开发

android--Service1(service不是一个单独的进程,也不是一个线程)

2011-04-09 21:36 537 查看
TestActivity.java

package zlj.service1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class TestActivity extends Activity {
/** Called when the activity is first created. */
private Button startService = null;
private Button stopService = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startService = (Button)findViewById(R.id.startService);
startService.setOnClickListener(new startServiceListener());
stopService = (Button)findViewById(R.id.stopService);
stopService.setOnClickListener(new stopServiceListener());

}
class startServiceListener implements OnClickListener{

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(TestActivity.this,FirstService.class);
startService(intent);

}
}
class stopServiceListener implements OnClickListener{

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(TestActivity.this,FirstService.class);
stopService(intent);

}
}
}


 

FirstService.java

package zlj.service1;

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

public class FirstService extends Service {

@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
System.out.println("Service onBind");
return null;
}

@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
System.out.println("Service onCreate");
}

@Override
public void onDestroy() {
// TODO Auto-generated method stub
System.out.println("Service onDestroy");
super.onDestroy();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
System.out.println("flag-->" + flags);
System.out.println("startId-->" + startId);
System.out.println("Service onStartCommand");
return START_NOT_STICKY;
}

}


 

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/startService"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="startService"
/>
<Button
android:id="@+id/stopService"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="stopService"
/>
</LinearLayout>


mainfest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="zlj.service1"
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=".TestActivity"
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=".FirstService"></service>

</application>
</manifest>


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息