一、環(huán)境
安卓系統(tǒng):4.2
操作系統(tǒng):Win 8.1
工具:Android Studio
二惠豺、利用廣播開啟后臺服務(wù)
定義一個廣播接收器類(MyBroadcastReceive)繼承自 BroadcastReceive,重寫里面的 onReceive 方法默责,即可在接收到廣播后做出相應(yīng)操作弛说。
public class MyBroadcastReceiver extends BroadcastReceiver {
private final String TAG = "MyBroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
//后邊的XXX.class就是要啟動的服務(wù)
Intent service = new Intent(context,NofyService.class);
context.startService(service);
Log.v(TAG, "onReceive");
}
}
定義一個服務(wù)(MyService)繼承自 Service ,并重寫 onCreate秕铛、onStartCommand、onDestroy 方法即可缩挑。onCreate 方法在服務(wù)被創(chuàng)建時調(diào)用但两,onStartCommand 在服務(wù)開始后調(diào)用,如果 Activity 多次調(diào)用 Service供置,則只會在第一次調(diào)用 onCreate谨湘,其他都是調(diào)用 onStartCommand。
public class NofyService extends Service {
private final String TAG = "NofyService";
private Intent in;
private int flags;
private int startId;
@Nullable
@Override
public IBinder onBind(Intent intent) { return null; }
@Override
public void onCreate() {
Toast.makeText(getApplication(), "service", Toast.LENGTH_SHORT).show();
Log.v(TAG, "onCreate");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
this.in = intent;
this.flags = flags;
this.startId = startId;
new Thread(checkTime).start();
Toast.makeText(getApplication(), "service",
Toast.LENGTH_SHORT).show();
Log.v(TAG, "onStart");
return START_STICKY;
}
//Service 的功能芥丧,創(chuàng)建通知欄通知
Runnable checkTime = new Runnable() {
@Override
public void run() {
Calendar ca = Calendar.getInstance();
//利用 Notification 類設(shè)置通知的屬性
Notification notification = new Notification.Builder(getApplicationContext())
.setContentTitle("事件提醒")
.setContentText("有事件")
//不設(shè)置小圖標(biāo)通知欄顯示通知(不確定)
.setSmallIcon(R.mipmap.ic_launcher)
.build();
notification.flags = Notification.FLAG_INSISTENT;
//利用 NotificationManager 類發(fā)出通知欄通知
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.notify(1, notification);
}
};
@Override
public void onDestroy() {
this.onStartCommand(in, flags, startId);
}
}
在 AndroidManifest.xml 注冊 MyBroadcastReceive紧阔。
<!-- 廣播接收器 -->
<receiver android:name=".broadcastReceiver.MyBroadcastReceiver">
<intent-filter >
<action android:name="com.cj_finger.broadcast" />
</intent-filter>
</receiver>
在 AndroidManifest.xml 注冊 MyService。
<!-- 服務(wù) -->
<service android:name=".service.NofyService" android:process=":NofyService" >
<intent-filter>
<action android:name="com.cj_finger.service.NofyService" />
</intent-filter>
</service>
*添加開機啟動權(quán)限(開機啟動娄柳,有的手機廠家會屏蔽開機廣播)寓辱。
<!-- 開機自啟權(quán)限 -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
*還要設(shè)置 MyBroadcastReceive 的注冊信息艘绍。
<!-- 廣播接收器 -->
<receiver android:name=".broadcastReceiver.BootBroadcastReceiver">
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</receiver>
最后編輯于 :2017.12.05 05:04:23
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者