几天以来,我一直在使用简单的警报管理器。警报管理器有望启动服务,但问题是它没有启动服务。
我的主要活动:
public class MainActivity extends Activity {
private Button start
private Button stop
@Override
protected void onCreate (Bundle _savedInstanceState){
super.onCreate (_savedInstanceState);
setContentView(R.layout.main);
initialize(_savedInstanceState);
initializeLogic();
}
private void initialize(Bundle _savedInstanceState) {
start = (Button) findByViewId(R.id.start);
stop = (Button) findByViewId(R.id.stop);
start.setOnClickListener(new View.onClickListener(){
@Override
public void onClick(View _view){
startAlert();
}});
stop.setOnClickListener(new View.onClickListener(){
@Override
public void onClick(View _view){
stopAlert();
}});
private void initializeLogic(){}
@Override
protected void onActivityResult(int _requestCode, int _resultCode, Intent _data){
super.onActivityResult(_requestCode, _resultCode, _data);
switch (_requestCode){
default:
break;
}}
public void startAlert() {
int i = 5;
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();
}
public void stopAlert() {
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
Toast.makeText(this, "Alarm stopped", Toast.LENGTH_LONG).show();
}
}
我的广播接收器:
public class MyBroadcastReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "Intent received", Toast.LENGTH_LONG).show();
Intent intent = new Intent(this, MyService.class);
context.startService(intent);
}
}
我的服务:
public class MyService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "onStartCommand Called", Toast.LENGTH_LONG).show();
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "onDestroy Called", Toast.LENGTH_LONG).show();
}
}
我已经在清单中注册了Service和BroadcastReceiver。在对stackoverflow提出了一些建议之后,我尝试了“ MyService”(不带。),但仍然无法正常工作。
<service android:name=".MyService" />
<receiver android:name=".MyBroadcastReceiver" />
我已经尝试了一些在此论坛或其他网站上可用的示例,但没有任何帮助。谁能强调我在做什么错?
答案 0 :(得分:0)
您可以尝试下面的代码,首先检查您是否已按照以下步骤注册了广播接收器和服务:
<service android:name="YOUR_PACKAGE_NAME.MyService"/>
<receiver android:name="YOUR_PACKAGE_NAME.MyBroadcastReceiver"/>
您还应该检查MainActivity.java类中是否缺少一些花括号。您可以将其与以下代码进行比较
public class MainActivity extends AppCompatActivity {
private Button start;
private Button stop;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize(savedInstanceState);
}
private void initialize(Bundle savedInstanceState)
{
start = (Button) findViewById(R.id.start);
stop = (Button) findViewById(R.id.stop);
start.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
startAlert();
}
});
stop.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
stopAlert();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode)
{
}
}
public void startAlert() {
int i = 5;
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();
}
public void stopAlert() {
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
Toast.makeText(this, "Alarm stopped", Toast.LENGTH_LONG).show();
}
}
最后,您需要在BroadcastReceiver类中进行一些小的调整,其中,意图对象的名称应更改为intent
之外的其他名称,如下所示:
public class MyBroadcastReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "Intent received", Toast.LENGTH_LONG).show();
Intent serviceIntent = new Intent(context, MyService.class);
context.startService(serviceIntent);
}
}
希望这可以解决您的问题。