所以这是我的alaram接收,它在应用程序被杀死时没有问题,通知被解雇,一切似乎都很好。
public class AlarmReciever extends BroadcastReceiver {
private CharSequence msg = "Notofication";
@Override
public void onReceive(Context context, Intent intent) {
Log.i("Alarm reciever", "Reached");
String sationSelected = intent.getStringExtra("SationSelected");
Log.i("slected is :", sationSelected);
Intent serviceRadio = new Intent(context, RadioService.class);
serviceRadio.putExtra("SationSelected",sationSelected);
context.startService(serviceRadio);
Intent dismissIntent = new Intent(context,DismissBroadcast.class);
dismissIntent.putExtra("Notification ID",1);
PendingIntent piDismiss = PendingIntent.getBroadcast(context, 100, dismissIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context)
.setOngoing(true)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Radio station playing")
.setAutoCancel(true)
.setContentText("Radio station playing content")
.setPriority(Notification.PRIORITY_MAX)
.setDefaults(Notification.DEFAULT_ALL)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.addAction(R.mipmap.ic_launcher_round,
"Stop Radio", piDismiss);
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(1, builder.build());
Log.e("You are at the","Receiver");
}
}
这是我的服务,我删除了一些站点,但留下了一些让你知道我在做什么,当应用程序被杀死时,这个服务似乎没有加载URL。如果app没有被杀死,那么这项服务工作正常。
public class RadioService extends Service implements MediaPlayer.OnPreparedListener {
MediaPlayer myPlayer;
Context context = this;
//// creat sation link variable////////////
String LBC = "http://media-ice.musicradio.com/LBCUKMP3Low";
String BBCRadio1 = "http://bbcmedia.ic.llnwd.net/stream/bbcmedia_radio1_mf_p";
String Kiss = "http://icy-e-ba-01-gos.sharp-stream.com/kissnational.aac";
String AbsoluteRadio = "http://mp3-ar-128.timlradio.co.uk/Title1=Absolute Radio 1215AM, Discover Real Music (High Quality) Length1=-1Version=2";
String AbsoluteClassicRock = "http://mp3-ac-32.timlradio.co.uk/Title1=Absolute Classic Rock - Home of the Great British Guarantee (Standard Quality) Length1=-1Version=2";
String Absolute80s = "http://mp3-a8-32.timlradio.co.uk/Title1=Absolute 80s (Standard Quality) Length1=-1Version=2";
String AbsoluteRadio60s = "http://mp3-a6-32.timlradio.co.uk/Title1=Absolute Radio 60s (Standard Quality) Length1=-1Version=2";
String AbsoluteRadio70s = "http://mp3-a7-32.timlradio.co.uk/Title1=Absolute Radio 70s (Standard Quality) Length1=-1Version=2";
String AbsoluteRadio90s = "http://mp3-a9-32.timlradio.co.uk/Title1=Absolute Radio 90s (Standard Quality) Length1=-1Version=2";
String AbsoluteRadio00s = "http://mp3-a0-32.timlradio.co.uk/Title1=Absolute Radio 00s (Standard Quality) Length1=-1Version=2";
String BBCRadio1xtra = "http://bbcmedia.ic.llnwd.net/stream/bbcmedia_radio1xtra_mf_p";
String BBCRadio2 = "http://bbcmedia.ic.llnwd.net/stream/bbcmedia_radio2_mf_p";
String BBCRadio3 = "http://bbcmedia.ic.llnwd.net/stream/bbcmedia_radio3_mf_p";
String BBCRadio4FM = "http://bbcmedia.ic.llnwd.net/stream/bbcmedia_radio4fm_mf_p";
String BBCRadio4Extra = "http://bbcmedia.ic.llnwd.net/stream/bbcmedia_radio4extra_mf_p";
String BBCRadio5Live = "http://bbcmedia.ic.llnwd.net/stream/bbcmedia_radio5live_mf_p";
String BBCRadio5LiveSportsballExtra = "http://bbcmedia.ic.llnwd.net/stream/bbcmedia_radio5extra_mf_p";
String BBCRadio6Music = "http://bbcmedia.ic.llnwd.net/stream/bbcmedia_6music_mf_p";
String BBCAsianNetwork = "http://bbcmedia.ic.llnwd.net/stream/bbcmedia_asianet_mf_p";
ArrayList<String> stationList = new ArrayList<>();
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
//WifiManager.WifiLock wifiLock = ((WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE)).createWifiLock(WifiManager.WIFI_MODE_FULL,"MyLockWIFI");
public int onStartCommand(Intent intent, int flags, int startId) {
//WifiManager.WifiLock wifiLock = ((WifiManager) getSystemService(WIFI_SERVICE))
// .createWifiLock(WifiManager.WIFI_MODE_FULL, "mylock");
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"partialWakrLock");
wakeLock.acquire();
Log.i("stt comand Radio recier","True");
///// get station selected////////////////////////////
String selectedStation = intent.getStringExtra("SationSelected");
Log.i("INtent selected = ",selectedStation);
//////// create new media player
myPlayer = new MediaPlayer();
//wifiLock.acquire();
myPlayer.setWakeMode(getApplicationContext(),PowerManager.PARTIAL_WAKE_LOCK);
myPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
myPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
//// Pass the selected sation to set stream///////////
try {
stationList.add("0");
stationList.add(LBC);
stationList.add(BBCRadio1);
stationList.add(AbsoluteRadio);
stationList.add(AbsoluteClassicRock);
stationList.add(Absolute80s);
stationList.add(AbsoluteRadio60s);
stationList.add(AbsoluteRadio70s);
stationList.add(AbsoluteRadio90s);
stationList.add(AbsoluteRadio00s);
stationList.add(BBCRadio1xtra);
stationList.add(BBCRadio2);
stationList.add(BBCRadio3);
stationList.add(BBCRadio4FM);
stationList.add(BBCRadio4Extra);
stationList.add(BBCRadio5Live);
stationList.add(BBCRadio5LiveSportsballExtra);
stationList.add(BBCRadio6Music);
stationList.add(BBCAsianNetwork);
stationList.add(BBCWorldServiceUKstream);
stationList.add(BBCWorldServiceNewsstream);
stationList.add(ClassicFM);
stationList.add(CapitalFM);
stationList.add(PlanetRock);
stationList.add(JazzFM);
stationList.add(RadioX);
stationList.add(HeartLondon);
stationList.add(SmoothFM);
stationList.add(RinseFM);
stationList.add(RadioCaroline);
String url = null;
if(!selectedStation.equals("Random")){
if (selectedStation.equals("LBC")) {
url = LBC;
Log.i("LBC = ", LBC);
}else{
if(selectedStation.equals("BBC")){
url = BBCRadio1;
Log.i("BBC = ", BBCRadio1);
}else{
if(selectedStation.equals("Kiss")){
url = Kiss;
Log.i("Kiss = ", Kiss);
}else{
if(selectedStation.equals("AbsoluteRadio")){
url = AbsoluteRadio;
}else{
if (selectedStation.equals("AbsoluteClassicRock")){
url = AbsoluteClassicRock;
}else{
if (selectedStation.equals("Absolute 80s")){
url = Absolute80s;
}else{
if (selectedStation.equals("AbsoluteRadio 60s")){
url = AbsoluteRadio60s;
}else{
if (selectedStation.equals("AbsoluteRadio 70s")){
url = AbsoluteRadio70s;
}else{
if (selectedStation.equals("AbsoluteRadio 90s")){
url = AbsoluteRadio90s;
}else{
if (selectedStation.equals("AbsoluteRadio 00s")){
url = AbsoluteRadio00s;
}else{
if (selectedStation.equals("BBC Radio 1xtra")){
url = BBCRadio1xtra;
}else{
if (selectedStation.equals("BBC Radio 2")){
url = BBCRadio2;
}else{
if (selectedStation.equals("BBC Radio 3")){
url = BBCRadio3;
}else{
if (selectedStation.equals("BBC Radio 4FM")){
url = BBCRadio4FM;
}else{
if (selectedStation.equals("BBC Radio 4Extra")){
url = BBCRadio4Extra;
}else{
if (selectedStation.equals("BBC Radio 5 Live")){
url = BBCRadio5Live;
}else{
if (selectedStation.equals("BBC Radio 5 Live Sportsball Extra")){
url = BBCRadio5LiveSportsballExtra;
}else{
if (selectedStation.equals("BBC Radio 6 Music")){
url = BBCRadio6Music;
}else{
if (selectedStation.equals("BBC Asian Network")){
url = BBCAsianNetwork;
}else{
if (selectedStation.equals("BBC World Service UK")){
url = BBCWorldServiceUKstream;
}else{
if (selectedStation.equals("BBC World Service News")){
url = BBCWorldServiceNewsstream;
}else{
if (selectedStation.equals("Classic FM")){
url = ClassicFM;
}else{
if (selectedStation.equals("Capital FM")){
url = CapitalFM;
}else{
if (selectedStation.equals("Planet Rock")){
url = PlanetRock;
}else{
if (selectedStation.equals("Jazz FM")){
url = JazzFM;
}else{
if (selectedStation.equals("Radio X")){
url = RadioX;
}else{
if (selectedStation.equals("Heart London")){
url = HeartLondon;
}else{
if (selectedStation.equals("Smooth FM")){
url = SmoothFM;
}else{
if (selectedStation.equals("Rinse FM")){
url = RinseFM;
}else{
if (selectedStation.equals("Radio Caroline")){
url = RadioCaroline;
}else{
if (selectedStation.equals("The Breeze Radio")){
url = TheBreezeRadio;
}else{
if (selectedStation.equals("Image FM")){
url = ImageFM;
}else{
if (selectedStation.equals("Max Radio")){
url = MaxRadio;
}else{
if (selectedStation.equals("Kane FM")){
url = KaneFM;
}else{
if (selectedStation.equals("Sunshine Radio")){
url = SunshineRadio;
}else{
if (selectedStation.equals("Talk Sport")){
url = TalkSport;
}else{
if (selectedStation.equals("Capital Extra")){
url = CapitalExtra;
}else{
if (selectedStation.equals("Magic")){
url = Magic;
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
myPlayer.setDataSource(url);
}else{
Random r = new Random();
int s = r.nextInt(stationList.size() - 1) + 1;
Log.i("S station = ", stationList.get(s));
myPlayer.setDataSource(stationList.get(s));
}
} catch (IOException e) {
e.printStackTrace();
}
//Set volume to max
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC),flags);
// Prepare myPlayer
myPlayer.prepareAsync();
Log.i("Check Wake lock = ", String.valueOf(wakeLock.isHeld()));
wakeLock.release();
//wifiLock.release();
return START_STICKY;
}
@Override
public void onPrepared(MediaPlayer mp) {
//Start myPlayer
mp.start();
}
public void onDestroy(){
//Stop and tidy up myPlayer
myPlayer.stop();
myPlayer.release();
myPlayer = null;
super.onDestroy();
}
/////////
}
和我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.radioalarm">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.media.browse.MediaBrowserService" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:process=":remote" android:name=".AlarmReciever"/>
<receiver android:name=".OnBootReciever">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<receiver android:name=".DismissBroadcast" />
<service android:name=".OnBootService"
android:enabled="true"/>
<service android:name=".RadioService"
android:enabled="true">
</service>
<activity android:name=".SavedAlarms" />
</application>
</manifest>
答案 0 :(得分:0)
我的预感是我的服务没有被调用。
CREATE TABLE Remaingdays1
(
id INT NOT NULL PRIMARY KEY,
SDate DATE,
EDate Date,
remaingdays as SDate-EDate-- This should be the resultant of days
);
和onStop()
放置日志,然后检查您的日志
还在onDestroye()
我已经在我的服务中实施了一个唤醒锁,但仍然没有快乐。 我还为媒体播放器实现了自己的唤醒锁定 仍然没有快乐。
无需实施唤醒锁定,只要服务正在运行,音乐就会播放
您也可以为音乐播放器学习GoogleProject。