Hello! I am building a project similar to an alarm clock but I am having some difficulties trying to use the broadcastreceiver class. Problem: I want to capture the date and the exact time that the cell phone screen was lit, that is, if the user just pressed the button to light the screen and see if it has notification, I would like to capture that moment and later insert it into the database. Likewise, I also want to capture the moment when the screen is deleted, practically following the same idea regarding the screen being connected, when it is deleted I want to take the system time to later save to the database.
But so far I've been able to use broadcastReceiver just to make the phone vibrate, in the example below I'm trying to turn on and unlock the screen, what I really want is to capture this moment from the user, but I can not even know where to start:
BroadcastActivity
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//EXEMPLO PARA FAZER O CELULAR VIBRAR
/*Toast.makeText(context, "Não entre em panico amigo!", Toast.LENGTH_SHORT).show();
//Vibrando o celular
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(2000);*/
//EXEMPLO PARA FAZER A TELA SER LIGADA E DESTRAVADA
Intent intentTela = new Intent(context, MainActivity.class);
intentTela.putExtra(MainActivity.EXTRA_LIGAR_E_DESTRAVAR_TELA, true);
context.startActivity(intentTela);
}
}
Sample screen:
public class MainActivity extends AppCompatActivity {
//ATRIBUTOS
EditText etTime;
Button btOk;
public static final String EXTRA_LIGAR_E_DESTRAVAR_TELA = ChamadaActivity.class.getPackage().getName() + ".LIGAR_E_DESTRAVAR_TELA";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (true == getIntent().getBooleanExtra(EXTRA_LIGAR_E_DESTRAVAR_TELA, false)){
//Combinando duas flags
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
}
}
public void configAlarm(View view){
etTime = findViewById(R.id.time);
int tempo = Integer.parseInt(etTime.getText().toString());
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 1, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (tempo * 1000), pendingIntent);
Toast.makeText(this, "Alarm marcado para " + tempo + " segundos", Toast.LENGTH_LONG).show();
}
}
xml main:
<?xml version="1.0" encoding="utf-8"?>
<EditText
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="number"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="32dp"
android:text="Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/time"
android:onClick="configAlarm"/>
</android.support.constraint.ConstraintLayout>
Manifest
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.VIBRATE" />
<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"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.INPUT_METHOD_CHANGED" />
</intent-filter>
</receiver>
<activity android:name=".ChamadaActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Thank you for your attention!