activity.java
public class MainActivity extends Activity {
private Button btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn=(Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,CloseService.class);
startService(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
service.java
public class CloseService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Intent newIntent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);//之所以能够在源码中查看,但是调用的时候不显示,是因为这个不对上层开放
newIntent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(newIntent);
return super.onStartCommand(intent, flags, startId);
}
}
AndroidManifest.xml
package="feng.f7_30.activity"
android:versionCode="1"
android:versionName="1.0"
android:sharedUserId="android.uid.system">
android:minSdkVersion="8"
android:targetSdkVersion="15" />
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:name=".MainActivity"
android:label="@string/title_activity_main" >
main.xml
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/btn_name" />
string.xml
添加一条
close
把这些都搞定了之后,照着上一篇的博客中写的,进行编译,将apk拷贝出来,install就ok了!