@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "++MainService onDestroy++");
flag = false;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "++MainService onCreate++");
Notification no = new Notification(R.drawable.ic_launcher, "有通知到来", System.currentTimeMillis());
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
no.setLatestEventInfo(this, "AIDLDemo", "running", pi);
startForeground(1, no);
}
@Override
public IBinder onBind(Intent intent) {
Bundle bundle = intent.getExtras();
flag = bundle.getBoolean("flag");
System.out.println(flag);
return ms;
}
MyServiceAIDL.Stub ms = new MyServiceAIDL.Stub() {
@Override
public void DownLoad() throws RemoteException {
new Thread(new Runnable() {
int i = 0;
@Override
public void run() {
//未达到线程条件,会一直在后台运行,就算服务已经关闭
while (flag) {
try {
i++;
System.out.println("i的值是" + i);
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("退出服务");
}
}).start();
}
};
}
AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".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
android:name=".MainService"
android:process=":remote"></service>
</application>
MyServiceAIDL.aidl
// myServiceAIDL.aidl
package com.example.wanghao.aidldemo;
// Declare any non-default types here with import statements
interface MyServiceAIDL {
void DownLoad();
}
Ubuntu 12.04(64位)安装Android Studio 全过程
Android Studio设置主题 和 不支持中文的问题解决方法