why BroadcastReceiver don't catch the Action? - java

Hi I am working on BroadCastReciver. There are two way to define BroadCastReciver.First one is using Java code and Second one is define in AndroidManifest.xml using . In my code second one is don't work properly.please tell where am i goes wrong.
public class HotelReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String dActionName = intent.getAction();
Log.i("My Rceiver ", intent.getAction());
if (dActionName.equals(Intent.ACTION_SCREEN_ON)) {
Toast.makeText(context, "SCREEN ON", Toast.LENGTH_SHORT).show();
} else if (dActionName.equals(Intent.ACTION_SCREEN_OFF)) {
}
}
}
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hotelsecurity"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="10"
android:maxSdkVersion="15" />
<uses-permission android:name="android.permission.PREVENT_POWER_KEY" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".HotelReceiver">
<intent-filter>
<action android:name="android.intent.action.SCREEN_ON"/>
</intent-filter>
</receiver>
</application>
</manifest>

I think your reciever should read
<receiver
android:name="HotelReceiver">
<intent-filter>
<action android:name="android.intent.action.SCREEN_ON"/>
</intent-filter>
</receiver>
no dot "."

Just use this code inside of onCreate() for your activity,
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
HotelReceiver mReceiver = new HotelReceiver(this);
registerReceiver(mReceiver, filter);

Related

How to change or set new activity launcher app? (in Java)

I have a payment activity and splash activity that it shows app of contents after user will buy it. payment activity is default Launcher in Manifest.xml that I want to set launcher Activity to splash activity and payment activity disabled after payment.
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exa.iu2">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<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=".PaymentActivity" android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="return" android:host=“zrp”/>
</intent-filter>
</activity>
<activity-alias
android:name=".PaymentActivity"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:enabled="false" android:targetActivity=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity-alias>
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#style/AppTheme.NoActionBar"/>
</application>
</manifest>
in Payment Activity and "If payment is success"
public void onCallbackResultVerificationPayment(boolean isPaymentSuccess, String refID, PaymentRequest paymentRequest) {
if(isPaymentSuccess){
Toast.makeText(PaymentActivity.this, “Payment Success” + refID, Toast.LENGTH_SHORT).show();
ComponentName cm = new ComponentName("com.exa.iu2", "com.exa.iu2"+".SplashActivity");
ComponentName cm2 = new ComponentName("com.exa.iu2", "com.exa.iu2"+".PaymentActivity");
PackageManager pm = getApplication().getPackageManager();
pm.setComponentEnabledSetting(cm, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,PackageManager.DONT_KILL_APP);
pm.setComponentEnabledSetting(cm2, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,0);
}else {
Toast.makeText(PaymentActivity.this, “Don’t success”, Toast.LENGTH_SHORT).show();
}
}
});
Thx
I suggest you create a Hub or Startup activity in which decide what to display. The new activity has to be set as Main -> Launcher activity, obviously.
In AndroidManifest do this
<activity android:name=".SplashActivity"
android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
Inside SplashActivity
public class SplashActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
// Check condition to open appropriate activity
// like
// if(paymentDone) start some activity
// else start another activity
finish()
}
}

Receiver in manifest file is giving error class or interface expected

There is an error in the manifest file that when I try to enter reciever it says class or interface expected
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.uzairaslam.silenter" >
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS_" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".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>
</application>
<receiver android:name="AlarmReceiver"/>
The receiver class which is not added is this
public class AlarmReceiver extends BroadcastReceiver {
SQLiteDatabase db;
String d ;
String deviceard;
String roomard ;
boolean status ;
public AlarmReceiver() {
}
#Override
public void onReceive(Context context, Intent intent) {
d= intent.getStringExtra("DeviceId");
deviceard = intent.getStringExtra("Device_Ard_Id");
roomard = intent.getStringExtra("Room_Ard_id");
status = Boolean.parseBoolean(intent.getStringExtra("Device_Status"));
if(status)
Send_sms("y"+roomard+":"+deviceard+":oz" ,context);
else
Send_sms("y"+roomard+":"+deviceard+":fz" ,context);
Toast.makeText(context,"Mateen",Toast.LENGTH_LONG).show();
}
public void Send_sms(String msg,Context c)
{
Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if (alarmUri == null) {
alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
}
Ringtone ringtone = RingtoneManager.getRingtone(c, alarmUri);
ringtone.play();
}
}
<receiver android:name="AlarmReceiver"/>
This line should be with in the application tag and give the fully qualified Receiver name. like below
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".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="<receiver package>.AlarmReceiver"/>
</application>

android Implement chatting through push notification using parse.com api

I am doing a chat application . In the app i have to do chatting with help of push notifications using parse andorid sdk. I am successfull to generate push notications between different users . But not able to recieve push and add their data in list view . Here is code of maifest file
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:name="example.chat.ChatApplication"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.facebook.LoginActivity"
android:label="#string/app_name" />
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="#string/app_id" />
<activity
android:name="example.chat.LoginActivity"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="example.chat.FriendslIst"
android:label="Friend list"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="example.chat.RegisterActivity"
android:label="#string/title_activity_register"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="example.chat.FriendListActivity"
android:label="#string/title_activity_friend_list"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="example.chat.ChatActivity"
android:label="#string/title_activity_chat"
android:screenOrientation="portrait" >
</activity>
<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.ParseBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.RECEIVE_BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
<receiver android:name="example.chat.MyCustomReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="example.chat.UPDATE_STATUS" />
</intent-filter>
</receiver>
</application>
and code for my custom reciever
public class MyCustomReceiver extends BroadcastReceiver {
private static final String TAG = "MyCustomReceiver";
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, ""+intent, Toast.LENGTH_LONG).show();
}
}
and from java code I am sending push like this :
ParseQuery<ParseInstallation> query = ParseInstallation.getQuery();
query.whereEqualTo("device_id", target);
ParsePush push = new ParsePush();
push.setQuery(query);
push.setMessage(message);
push.setExpirationTimeInterval(86400);
push.sendInBackground();
Please tell me where I am wrong for recieving data using reciever and what to do when i recive push means any logic or idea to move further . Thanks in advance
here is the manifiest file.....
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="your package name"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<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="your package name.YourActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.ParseBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.RECEIVE_BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
<receiver android:name="your package name.YourCustomReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="your package name.UPDATE_STATUS" />
</intent-filter>
</receiver>
</application>
</manifest>
use this code to receive the push...
public class MyCustomReceiver extends BroadcastReceiver {
String title, from, msg;
#Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
String message = extras != null ? extras.getString("com.parse.Data")
: "";
Log.e("message ", " " + message);
JSONObject jObject;
try {
if (message != null && !message.equals("")) {
jObject = new JSONObject(message);
from = jObject.getString("from");
msg = jObject.getString("title");
title = jObject.getString("msg");
GCMMessage gcmMessage = new GCMMessage();
gcmMessage.setMsg_body(msg);
gcmMessage.setMsg_title(title);
gcmMessage.setType(0);
gcmMessage.setDateTime(time);
DatabaseUtil.insertMessage(context, gcmMessage);
}
}
catch (JSONException e) {
e.printStackTrace();
}
}
}
and here is the code to send push...
JSONObject obj;
try {
obj =new JSONObject();
obj.put("alert","oman expert ");
obj.put("action","Your Package name.UPDATE_STATUS");
data.put("from", ParseUser.getCurrentUser().getUsername());
obj.put("msg","hi");
obj.put("title","msg");
ParsePush push = new ParsePush();
ParseQuery query = ParseInstallation.getQuery();
push.setQuery(query);
push.setData(obj);
push.sendInBackground();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for any further query please let me know....
In the above code example, you have missed out few things,
Check your AndroidManifest.xml file, and add c2dm permissions,
permission android:name="your package.permission.C2D_MESSAGE" android:protectionLevel="signature"
uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"
uses-permission android:name="your package.permission.C2D_MESSAGE"
While sending the data, set the action in your data,
JSONObject data = new JSONObject();
try {
data.put("action", "your package.GOT_MESSAGE");
data.put("ya", ya);
data.put("from", ParseUser.getCurrentUser().getUsername());
}catch (Exception e){
e.printStackTrace();
return;
}
ParsePush parsePush = new ParsePush();
parsePush.setData(data);

android application working but crash after work done

my app is about turning phone on ringer mode via sms received but it crashes after its work is completed showing "Unfortunately, your app has stopped responding..". Here is my manifest and receiver.java file
My manifest file
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hide"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.hide.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>
<activity
android:name="com.example.hide.Info"
android:label="#string/title_activity_info" >
<intent-filter>
<action android:name="com.example.hide.Info" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.hide.Help"
android:label="#string/title_activity_help" >
<intent-filter>
<action android:name="com.example.hide.Help" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.hide.Next"
android:label="#string/title_activity_next" >
<intent-filter>
<action android:name="com.example.hide.Next" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<receiver android:name="com.example.hide.MyReceiver">
<intent-filter android:priority="100">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
<action android:name="android.media.RINGER_MODE_CHANGED"/>
</intent-filter>
</receiver>
</application>
MYReceiver.java
public class MyReceiver extends BroadcastReceiver{
LocationManager lm;
LocationListener locationListener;
String sender;
#Override
public void onReceive(Context context, Intent intent) {
SmsMessage[] sms = null;
Bundle b = intent.getExtras();
String str = " SMS From ";
if (b != null) {
Object[] pdus = (Object[]) b.get("pdus");
sms = new SmsMessage[pdus.length];
for (int i = 0; i < sms.length; i++) {
sms[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
if (i == 0) {
sender += sms[i].getOriginatingAddress();
str += ":"+sms[i].getMessageBody().toString();
if (sms[i].getMessageBody().equals("Ring")) {
AudioManager am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
int maxVolume = am.getStreamMaxVolume(AudioManager.STREAM_RING);
am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
am.setStreamVolume(AudioManager.STREAM_RING, maxVolume,AudioManager.FLAG_SHOW_UI + AudioManager.FLAG_PLAY_SOUND);
}
}
}
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
Log.d("Receiving", str);
}
}
}
Further more caused in log is showing NULLPOINTEREXCEPTION(), but i didn't get there is no code that throws exception.
But it is working fine as well it shows unfortunate message whenever i receive message on other app also.

android unable to start receiver not yet implemented

I am wanting to start a service (CheckTime) when the phone finishes booting. This appears to work, but my app immediately crashes. I get the following error "java.lang.RuntimeException: Unable to start receiver com.example.alarmtest.MyReceiver: java.lang.UnsupportedOperationException: Not yet implemented". I have been unsuccessful in my attempts to solve this problem. I am unsure if I have left something out of the manifest or not (I am fairly new to developing for android).
This is my code for MyReceiver. (the log.d for this appears)
public class MyReceiver extends BroadcastReceiver {
public MyReceiver() {
}
#Override
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent(context, CheckTime.class);
context.startService(startServiceIntent);
Log.d("evan alarm", "Started on Launch, android autostart");
throw new UnsupportedOperationException("Not yet implemented");
}
}
and the code for CheckTime (the log.d for this never appears)
public class CheckTime {
public void loglab(){
Log.d("evan alarm", "We are now in CheckTime");
}
}
and finally the manifest.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.alarmtest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<service
android:name="com.example.alarmtest.MyService" >
<intent-filter
android:label="com.example.alarmtest.MyService">
</intent-filter>
</service>
<service android:name="com.example.alarmtest.CheckTime" />
<activity
android:name="com.example.alarmtest.AlarmActivity"
android:label="#string/title_activity_alarm" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.alarmtest.ChangeTime"
android:label="#string/title_activity_change_time" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.AlarmTest.AlarmActivity" />
</activity>
<receiver
android:name="com.example.alarmtest.MyReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
throw new UnsupportedOperationException("Not yet implemented");
That's the last line of your receiver. You're throwing it yourself.
remove this line
throw new UnsupportedOperationException("Not yet implemented");

Categories