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

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="feng.f7_30.activity"

android:versionCode="1"

android:versionName="1.0"

android:sharedUserId="android.uid.system">

<uses-sdk

android:minSdkVersion="8"

android:targetSdkVersion="15" />

<uses-permission android:name="android.permission.SHUTDOWN"/>

<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="CloseService"></service>

</application>

</manifest>

main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent" >

<Button

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" />

</RelativeLayout>

string.xml

添加一条

<resources>

<string name="btn_name">close</string>

</resources>


把这些都搞定了之后,照着上一篇的博客中写的,进行编译,将apk拷贝出来,install就ok了!

更多相关文章

  1. Android的GLSurfaceView测试源码
  2. Android 源码编译问题集锦
  3. Android模仿微信导航页效果源码
  4. Android_Google android SDK源码(1)
  5. 近百android程序源码贡献 转载的自己看的
  6. android 音乐播放器源码
  7. Android查看jar包源码问题

随机推荐

  1. 移动布局原理、实战手机页面的基本整体架
  2. 仿PHP中文网首页
  3. Android(安卓)Studio decide the exact v
  4. 调用android自带的截图工具进行截图
  5. Android的TCP Socket连接组件
  6. android 中如何解析Rss订阅的xml文件
  7. android新手学习帮助网址
  8. android 图片灰度处理的处理
  9. Android(安卓)DownloadManager下载并安装
  10. network: android 网络判断