我试图在android中创建自己的闹钟。目的是我的闹钟应该根据我的播放列表播放不同的歌曲。这是一个大学项目。用户设置警报然后忘记,然后甚至在多次重启后,警报应该播放。我从中下载了一个示例代码并正在处理此问题。然而,在重新启动手机时,我的警报就会丢失。我发现我需要使用广播接收器,但仍然无法使用我的手机熔岩手机。你能否建议这段代码是否合适。这是我的代码链接:https://drive.google.com/file/d/1ADVyH0grEQ4g80HkFU1n2Cb4wxnQC4TX/view?usp=sharing
我的android清单文件是..
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.alarmmanager">
<uses-permission android:name="android.permission.VIBRATE" />
<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" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="MyBroadcastReceiver" >
</receiver>
</application>
</manifest>
我的布局文件......
<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"
tools:context=".MainActivity" >
<EditText
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="28dp"
android:ems="10"
android:hint="Number of seconds"
android:inputType="numberDecimal" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/time"
android:layout_below="@+id/time"
android:layout_marginRight="60dp"
android:layout_marginTop="120dp"
android:text="Start" />
</RelativeLayout>
我的收件人代码..
package com.alarmmanager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.util.Log;
import android.widget.Toast;
import java.util.Calendar;
/**
* Created by bhaskar on 24-04-2018.
*
*/
public class MyBroadcastReceiver extends BroadcastReceiver {
MediaPlayer mp;
@Override
public void onReceive(Context context, Intent intent) {
mp= MediaPlayer.create(context, R.raw.alrm );
mp.start();
Toast.makeText(context, "Alarm....", Toast.LENGTH_LONG).show();
Log.i("Alarm","Alarm"+ Calendar.getInstance().getTime());
}
}
mainActivity文件
public class MainActivity extends AppCompatActivity {
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button) findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startAlert();
}
});
}
public void startAlert() {
EditText text = (EditText) findViewById(R.id.time);
int i = Integer.parseInt(text.getText().toString());
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ (i * 1000), pendingIntent);
Toast.makeText(this, "Alarm set in " + i + " seconds", Toast.LENGTH_LONG).show();
}
}
答案 0 :(得分:1)
请参阅此official doc,以便在重新启动手机时保持警报。 在设备启动时启动警报部分
获取实际代码后,我正在编辑此答案:
1)创建新的Receiver而不是使用旧的接收器
init: function () {
events.doClickFunctions();
},
login: {
success: function () {
//here should be the linking done
mainView.router.load('/homepage/');
}
}
};
2)在清单文件中注册:
public class SampleBootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
// Set the alarm here.
Log.d("AlarmDone", "AlarmDone");
}
}
}
3)更改旧的接收器代码:
<receiver
android:name=".SampleBootReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
注意:在清单中添加权限
<receiver android:name=".AlarmReceiver" />
现在闹响了。
答案 1 :(得分:0)
对我而言,只有在我启用自动启动
时才有效