Android启动Service

最近项目中用到Service,我去,头一次接触这玩意啊。。。


1、

public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


Intent intent = new Intent();
                intent.setAction("com.example.fsdserialport.startService"); 
                MainActivity.this.startService(intent);
}


}


2、

public class StartService extends Service{

private Timer timer = new Timer();

@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}

@Override
public void onCreate() {
// TODO Auto-generated method stub
Log.d("fsd-Service", "onCreate");

      timer.schedule(task, 0, 300); //启动一个timer

super.onCreate();

}


@Override
public void onDestroy() {
// TODO Auto-generated method stub
Log.d("Service", "onDestroy");
super.onDestroy();
}


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Log.d("Service", "onStartCommand");
return super.onStartCommand(intent, flags, startId);
}

private TimerTask task = new TimerTask() {
public void run() {
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}
}; 
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:


break;
}
super.handleMessage(msg);
}
};

}


3、

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.fsdserialport"
    android:versionCode="1"
    android:versionName="1.0" >


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


     <!-- 开机自动启动权限 -->
     <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.fsdserialport.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 -->
         <service android:name="com.example.fsdserialport.StartService"
             android:permission="oem.permission.SENDMAIL"
             >
          <intent-filter>
                <action android:name="com.example.fsdserialport.startService"/>
            </intent-filter>
        </service>
        
    </application>
    <!-- service权限 -->
    <permission android:protectionLevel="normal" android:name="oem.permission.SENDMAIL">
        
    </permission>
</manifest>


加红处,如果没有的话,可能会有问题;这个我也是初次玩耍

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值