Reputation: 498
I have registered my Receiver in mainfist XML, as shown below:
<?xml version="1.0" encoding="UTF-8"?>
<manifest android:versionName="1.0"
android:versionCode="1"
package="com.eiq.randomsendriod"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.eiq.randomsendriod.SendRandom"
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="myreceiver">
<intent-filter>
<action android:name="com.esmaeel.incomIM">
</action>
</intent-filter>
</receiver>
<service android:name=".seekNet"></service>
</application>
</manifest>
declared My BroadCastReceiver:
public BroadcastReceiver myreceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"I have raceived a broadcast",Toast.LENGTH_SHORT).show();
Log.e("SEMOAS","In onReceive(...)");
Bundle bundle = intent.getExtras();
if (bundle != null) {
incom.setBody(bundle.get("body").toString());
incom.setFrom(bundle.get("From").toString());
incom.setTo(bundle.get("To").toString());
incom.setTogender(bundle.get("toGender").toString());
incom.my_gender=bundle.get("SenderGender").toString();
incom.type=bundle.getInt("msgType");
incom.url=bundle.get("url").toString();
incom.userID=bundle.get("SenderID").toString();
Log.e("SEMOA","Message ariived to activity via Brodcasting");
Toast.makeText(context,"Congratulations!!! got message: "+ incom.getBody(),Toast.LENGTH_LONG).show();
}
Log.e("SEMOA","Message ariived to activity via Brodcasting-----null");
}
};
and Sending broadcast from my Service:
Timer t = new Timer();
t.scheduleAtFixedRate( new TimerTask() {
@Override
public void run() {
Log.e("SEMOA", "IN RUNNABLE");
try {
if ((incom = (Message) SendRandom.ObjectInServercht.readUnshared()) == null)
Log.e("ESMOA","No incumming messages!!!");
else {
myI.putExtra("body", incom.getBody());// myI is an intent
myI.putExtra("From", incom.getFrom());
myI.putExtra("To", incom.getTo());
myI.putExtra("toGender", incom.getTogender());
myI.putExtra("SenderGender", incom.my_gender);
myI.putExtra("msgType", incom.type);
myI.putExtra("url", incom.url);
myI.putExtra("SenderID", incom.userID);
myI.setAction("com.esmaeel.incomIM");
try{
seekNet.this.sendBroadcast(myI);
Log.e("ESMOA","sendBroadcast!!");
}catch(Exception e){
Log.e("ESMOA","not sent sendBroadcast!! "+ e.toString());
}
}
the send part Success, I see: Log.e("ESMOA","sendBroadcast!!");
result in logcat,
but the receive function (onReceive) never run!
In the XML manifest file: <receiver android:name="myreceiver">
, the value "myreceiver" apeare in red and unresolved!
May you point on the error?
Upvotes: 0
Views: 138
Reputation: 4314
Manifest: add this permission:
<uses-permission android:name="android.permission.WAKE_LOCK" />
and also as you've already done add receiver:
<receiver android:name="PACKAGE.AlarmReciever" />
now build your AlarmReciever:
public class AlarmReciever extends BroadcastReceiver {
Context mContext;
@Override
public void onReceive(Context context, Intent intent) {
this.mContext = context;
try {
// Do whatever you want here
} catch (Exception e) {
e.printStackTrace();
}
}
Then you can call it in your code like this :
Intent intent = new Intent(mContext, AlarmReciever.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
AlarmManager alarmManager = (AlarmManager) mContext
.getSystemService(Context.ALARM_SERVICE);
Calendar instance = Calendar.getInstance();
instance.add(Calendar.SECOND, (int) MIL-YOU-WANT-TO-WAKE-UP / 1000);
alarmManager.set(AlarmManager.RTC_WAKEUP, instance.getTimeInMillis(),
pendingIntent);
Hope it helps :)
Upvotes: 0
Reputation: 39191
You're not implementing your receiver correctly. To register the receiver in the manifest, it needs to be a subclass of BroadcastReceiver, and not an instance of BroadcastReceiver in the Activity. You need to create a receiver class that extends BroadcastReceiver, like so:
public class MyReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context,"I have raceived a broadcast", Toast.LENGTH_SHORT).show();
Log.e("SEMOAS","In onReceive(...)");
...
This needs to be in its own file, MyReceiver.java.
Your manifest would need to be changed to:
<receiver android:name="MyReceiver">
<intent-filter>
<action android:name="com.esmaeel.incomIM">
</action>
</intent-filter>
</receiver>
If, on the other hand, you only need your receiver to be active while your Activity is running, you would register it in the Activity with the registerReceiver()
method. In this case, you would remove the receiver
entry from your manifest, and keep the rest of the Activity code as it is.
Upvotes: 1