AlarmManager 不是 运行
AlarmManager not running
我正在尝试创建一个更复杂的应用程序,但无法正确使用 AlarmManager 打开 activity。我尝试将代码提炼到我所缺少的本质,但它仍然无法正常工作。
该应用程序基本上只接受分钟和小时输入以及开始按钮,并且应该在输入时间时打开第二个 activity。但是,第二个 activity 在我输入时从未打开,LogCat 中的任何内容似乎都没有改变。这是我当前的代码:
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.goali.adalarm">
<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=".AlarmReceiver2" >
<intent-filter>
<action android:name="android.intent.alarmclock.ALARM_ALERT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<activity android:name=".AlarmReceiver">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
主要活动:
public class MainActivity extends AppCompatActivity {
Calendar c;
PendingIntent pendingIntent;
AlarmManager alarmManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
c = Calendar.getInstance();
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
final EditText hour = (EditText) findViewById(R.id.hour);
final EditText minute = (EditText) findViewById(R.id.minute);
Button button = (Button) findViewById(R.id.press);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
c.set(Calendar.HOUR_OF_DAY, (Integer.parseInt(hour.getText().toString())));
c.set(Calendar.MINUTE, Integer.parseInt(minute.getText().toString()));
c.set(Calendar.SECOND,0);
c.set(Calendar.MILLISECOND,0);
Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);
}
Toast.makeText(MainActivity.this, "Alarm Set.", LENGTH_LONG).show();
}
});
}
}
第二个activity:
public class AlarmReceiver extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm_receiver);
}
}
广播接收器:
public class AlarmReceiver2 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent intent2 = new Intent(context, AlarmReceiver.class);
Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();
context.startActivity(intent2);
}
}
根据你的代码,我猜你想在一段时间后使用 AlarmManager
.
开始 Activity
或 Broadcast
启动一个activity(AlarmReceiver)。
public class MainActivity extends AppCompatActivity {
PendingIntent pendingIntent;
AlarmManager alarmManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
final EditText hour = (EditText) findViewById(R.id.hour);
final EditText minute = (EditText) findViewById(R.id.minute);
Button button = (Button) findViewById(R.id.press);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int hours = Integer.parseInt(hour.getText().toString());
int minutes = Integer.parseInt(minute.getText().toString());
Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
// 1 hour = 60 minutes = 3600 seconds = 3600000 milliseconds.
long timeFromNowInMilliseconds = (hours * 3600000) + (minutes * 60000);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + timeFromNowInMilliseconds, pendingIntent);
}
Toast.makeText(MainActivity.this, "Alarm Set.", LENGTH_LONG).show();
}
});
}
}
开始广播(AlarmReceiver2)
public class MainActivity extends AppCompatActivity {
PendingIntent pendingIntent;
AlarmManager alarmManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
final EditText hour = (EditText) findViewById(R.id.hour);
final EditText minute = (EditText) findViewById(R.id.minute);
Button button = (Button) findViewById(R.id.press);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int hours = Integer.parseInt(hour.getText().toString());
int minutes = Integer.parseInt(minute.getText().toString());
Intent intent = new Intent(MainActivity.this, AlarmReceiver2.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
// 1 hour = 60 minutes = 3600 seconds = 3600000 milliseconds.
long timeFromNowInMilliseconds = (hours * 3600000) + (minutes * 60000);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + timeFromNowInMilliseconds, pendingIntent);
}
Toast.makeText(MainActivity.this, "Alarm Set.", LENGTH_LONG).show();
}
});
}
}
我正在尝试创建一个更复杂的应用程序,但无法正确使用 AlarmManager 打开 activity。我尝试将代码提炼到我所缺少的本质,但它仍然无法正常工作。
该应用程序基本上只接受分钟和小时输入以及开始按钮,并且应该在输入时间时打开第二个 activity。但是,第二个 activity 在我输入时从未打开,LogCat 中的任何内容似乎都没有改变。这是我当前的代码:
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.goali.adalarm">
<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=".AlarmReceiver2" >
<intent-filter>
<action android:name="android.intent.alarmclock.ALARM_ALERT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<activity android:name=".AlarmReceiver">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
主要活动:
public class MainActivity extends AppCompatActivity {
Calendar c;
PendingIntent pendingIntent;
AlarmManager alarmManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
c = Calendar.getInstance();
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
final EditText hour = (EditText) findViewById(R.id.hour);
final EditText minute = (EditText) findViewById(R.id.minute);
Button button = (Button) findViewById(R.id.press);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
c.set(Calendar.HOUR_OF_DAY, (Integer.parseInt(hour.getText().toString())));
c.set(Calendar.MINUTE, Integer.parseInt(minute.getText().toString()));
c.set(Calendar.SECOND,0);
c.set(Calendar.MILLISECOND,0);
Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);
}
Toast.makeText(MainActivity.this, "Alarm Set.", LENGTH_LONG).show();
}
});
}
}
第二个activity:
public class AlarmReceiver extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm_receiver);
}
}
广播接收器:
public class AlarmReceiver2 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent intent2 = new Intent(context, AlarmReceiver.class);
Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();
context.startActivity(intent2);
}
}
根据你的代码,我猜你想在一段时间后使用 AlarmManager
.
Activity
或 Broadcast
启动一个activity(AlarmReceiver)。
public class MainActivity extends AppCompatActivity {
PendingIntent pendingIntent;
AlarmManager alarmManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
final EditText hour = (EditText) findViewById(R.id.hour);
final EditText minute = (EditText) findViewById(R.id.minute);
Button button = (Button) findViewById(R.id.press);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int hours = Integer.parseInt(hour.getText().toString());
int minutes = Integer.parseInt(minute.getText().toString());
Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
// 1 hour = 60 minutes = 3600 seconds = 3600000 milliseconds.
long timeFromNowInMilliseconds = (hours * 3600000) + (minutes * 60000);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + timeFromNowInMilliseconds, pendingIntent);
}
Toast.makeText(MainActivity.this, "Alarm Set.", LENGTH_LONG).show();
}
});
}
}
开始广播(AlarmReceiver2)
public class MainActivity extends AppCompatActivity {
PendingIntent pendingIntent;
AlarmManager alarmManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
final EditText hour = (EditText) findViewById(R.id.hour);
final EditText minute = (EditText) findViewById(R.id.minute);
Button button = (Button) findViewById(R.id.press);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int hours = Integer.parseInt(hour.getText().toString());
int minutes = Integer.parseInt(minute.getText().toString());
Intent intent = new Intent(MainActivity.this, AlarmReceiver2.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
// 1 hour = 60 minutes = 3600 seconds = 3600000 milliseconds.
long timeFromNowInMilliseconds = (hours * 3600000) + (minutes * 60000);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + timeFromNowInMilliseconds, pendingIntent);
}
Toast.makeText(MainActivity.this, "Alarm Set.", LENGTH_LONG).show();
}
});
}
}