0

I'm trying to create a Service that checks every x minutes for new messages on a server.

I was quit happy to found this post : Alarm Manager Example

MainActivity

package com.example.alarmexample;  

import android.app.Activity;  
import android.app.AlarmManager;  
import android.app.PendingIntent;  
import android.content.Intent;  
import android.os.Bundle;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.EditText;  
import android.widget.Toast;  

public class MainActivity extends Activity {  
Button b1;  

    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main); 

        startAlert();  

}   public void startAlert() { 
        int timeInSec = 2;

        Intent intent = new Intent(this, MyBroadcastReceiver.class);  
        PendingIntent pendingIntent = PendingIntent.getBroadcast(  
                                      this.getApplicationContext(), 234, intent, 0);  
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);  
        alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (timeInSec * 1000), pendingIntent);  
        Toast.makeText(this, "Alarm set to after " + i + " seconds",Toast.LENGTH_LONG).show();  
    }  

}

MyBroadcastReceiver.java

import android.content.BroadcastReceiver;  
import android.content.Context;  
import android.content.Intent;  
import android.media.MediaPlayer;  
import android.widget.Toast;  

public class MyBroadcastReceiver extends BroadcastReceiver {  
    MediaPlayer mp;  
    @Override  
    public void onReceive(Context context, Intent intent) {  
        mp=MediaPlayer.create(context, R.raw.alarm);  
        mp.start();  
        Toast.makeText(context, "Alarm", Toast.LENGTH_LONG).show();  
    }  
}  

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>  
<manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    package="com.example.alarmexample" >  

    <uses-permission android:name="android.permission.VIBRATE" />  


    <application  
        android:allowBackup="true"  
        android:icon="@drawable/ic_launcher"  
        android:label="@string/app_name"  
        android:theme="@style/AppTheme" >  

        <activity  
            android:name="com.example.alarmexample.MainActivity"  
            android:label="@string/app_name" >  
            <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>  

This does exact the thing I want. But if I try to use the Context from the BroadcastReceiver-class to notify the user. The App doesn't starts when the user clicks on the notification.

Is there something spezial I have to do to archive this ?

Meeresgott
  • 431
  • 3
  • 17

2 Answers2

1

This is not a direct answer to your question (cant comment because not enough reputation) , but do you really need to check the server every x minutes for an event? I dont know anything about your project, but firebase https://firebase.google.com/docs/cloud-messaging/ would probably be a good alternative. It allows you to send Push Notifications to your client so that the client doesnt need to poll constantly.

Jan Meyer
  • 214
  • 3
  • 10
  • unfortunately yes, I can't afford the cost for calculation from every device, yet . I know it is a no no to add a relative expensive calculation to such a service, but this service is optional and I'll change this in the future but for now I have to go this way. – Meeresgott Oct 07 '19 at 21:20
0

Open application after clicking on Notification

Here is the answer - I was looking at the wrong end from this problem.

Meeresgott
  • 431
  • 3
  • 17