BroadcastReceiver not working for PendingIntent(...) - android

I am developing an SMS messaging application. The problem arose with BroadcastReceiver which check sent message or no. When I registrer him in activity, he works good. But I want to saddle him in the department class so that he will not be destroyed in the event of the closure of activity. Please help =(
public void send(View view) {
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
msg = mEditMessage.getText().toString();
SmsManager sm = SmsManager.getDefault();
ArrayList<String> parts = sm.divideMessage(msg);
Intent iSent = new Intent(SENT);
iSent.putExtra("id", lastId + 1);
PendingIntent piSent = PendingIntent.getBroadcast(this, 0, iSent, 0);
Intent iDel = new Intent(DELIVERED);
iDel.putExtra("msg", msg);
PendingIntent piDel = PendingIntent.getBroadcast(this, 0, iDel, 0);
progressSms = new Sms(lastId + 1, extraAddress, msg, "1", Calendar.getInstance().getTime(), 2, imageUri, extraName, true);
updateAdapter(progressSms);
if (parts.size() == 1)
{
msg = parts.get(0);
sm.sendTextMessage(extraAddress, null, msg, piSent, piDel);
compileMessage();
}
else
{
ArrayList<PendingIntent> sentPis = new ArrayList<PendingIntent>();
ArrayList<PendingIntent> delPis = new ArrayList<PendingIntent>();
int ct = parts.size();
for (int i = 0; i < ct; i++)
{
sentPis.add(i, piSent);
delPis.add(i, piDel);
}
sm.sendMultipartTextMessage(extraAddress, null, parts, sentPis, delPis);
public class SentSmsReciver extends BroadcastReceiver {//
private String LOG_TAG = "sent_reciver";
private SentListener sentListener;
public SentSmsReciver(SentListener sentListener) {
Log.i(LOG_TAG, "create reciver");
this.sentListener = sentListener;
}
private boolean failed = true;
private Context context;
private String id;
private SQLiteDatabase db;
#Override
public void onReceive(Context context, Intent intent) {
Log.i(LOG_TAG, "create reciver");
this.context = context;
db = new SmsDbHelper(context.getApplicationContext()).getWritableDatabase();
id = intent.getStringExtra("id");
switch(getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(context,"SMS sent",
Toast.LENGTH_SHORT).show();
failed = false;
compileMessage();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(context,"Generic failure",
Toast.LENGTH_SHORT).show();
compileMessage();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(context,"No service",
Toast.LENGTH_SHORT).show();
compileMessage();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(context,"Null PDU",
Toast.LENGTH_SHORT).show();
compileMessage();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(context,"Radio off",
Toast.LENGTH_SHORT).show();
compileMessage();
break;
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my.ciphermessanger">
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.READ_SMS" />
<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">
<service
android:name=".activity.MyIntentService"
android:exported="false"></service>
<receiver
android:name=".recivers.SentSmsReciver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="SMS_SENT" />
</intent-filter>
</receiver>
<receiver
android:name=".recivers.DeliverSmsReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="SMS_DELIVERED" />
</intent-filter>
</receiver>
<activity android:name=".activity.DialogActivity" />
<activity android:name=".activity.SmsListActivity" />
<receiver
android:name=".recivers.SmsReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_DELIVER" />
</intent-filter>
</receiver>
<receiver
android:name=".recivers.MmsReceiver"
android:permission="android.permission.BROADCAST_WAP_PUSH">
<intent-filter>
<action android:name="android.provider.Telephony.WAP_PUSH_DELIVER" />
<data android:mimeType="application/vnd.wap.mms-message" />
</intent-filter>
</receiver>
<activity android:name=".activity.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".services.HeadlessSmsSendService"
android:exported="true"
android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE">
<intent-filter>
<action android:name="android.intent.action.RESPOND_VIA_MESSAGE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="sms" />
<data android:scheme="smsto" />
<data android:scheme="mms" />
<data android:scheme="mmsto" />
</intent-filter>
</service>
<activity android:name=".activity.ComposeSmsActvity">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.SENDTO" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="sms" />
<data android:scheme="smsto" />
<data android:scheme="mms" />
<data android:scheme="mmsto" />
</intent-filter>
</activity>
</application>
</manifest>

if someone will be mean
Start service and there register broadcast
public class MyIntentService extends Service {
String SENT = "SMS_SENT";
BroadcastReceiver broadcastReceiver;
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
broadcastReceiver = new SentSmsReciver();
registerReceiver(broadcastReceiver, new IntentFilter(SENT));
}
#Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(broadcastReceiver);
}
}

Related

Newly receive message is not retrieving from my own Default SMS App Inbox

I am working on a project in which I have made app to default sms app and getting new message body and notification through Broadcast Receiver. It is showing toast that new message has been received and also read new message body.
But problems are
Problem 1:
The newly received sms is not retrieving from my default sms app inbox and not showing in my listview.
Problem 2:
I am not able to get each and every message from each conversation
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.e(TAG, "Intent recieved: " + intent.getAction());
if (intent.getAction() == SMS_RECEIVED) {
SmsMessage msg = null;
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdus = (Object[])bundle.get("pdus");
final SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
}
if (messages.length > -1) {
Log.e(TAG, "Message recieved: " + messages[0].getMessageBody());
MyNotificationManager.getInstance(context).displayNotification(messages[0].getOriginatingAddress(), messages[0].getMessageBody());
}
}
}
}
Manifest File
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.WRITE_SMS"/>
<uses-sdk android:name="org.apache.http.legacy" android:required="false"/>
<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"
android:name="com.ughareja.whocaller.utils.App"
android:networkSecurityConfig="#xml/network_security_config"
android:largeHeap="true">
<activity android:name="com.ughareja.whocaller.activities.SplashActivity"
android:screenOrientation="portrait"
android:theme="#style/Theme.AppCompat.Light.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="#string/default_notification_channel_id" />
<receiver android:name=".smsReciever.SmsListener"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
<intent-filter>
<action android:name="android.provider.Telephony.SMS_DELIVER" />
</intent-filter>
</receiver>
<!-- BroadcastReceiver that listens for incoming MMS messages -->
<receiver android:name=".smsReciever.MmsReciever"
android:permission="android.permission.BROADCAST_WAP_PUSH">
<intent-filter>
<action android:name="android.provider.Telephony.WAP_PUSH_DELIVER" />
<data android:mimeType="application/vnd.wap.mms-message" />
</intent-filter>
</receiver>
<!-- Activity that allows the user to send new SMS/MMS messages -->
<activity android:name=".smsReciever.ComposeSmsActivity" >
<intent-filter>
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.SENDTO" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="sms" />
<data android:scheme="smsto" />
<data android:scheme="mms" />
<data android:scheme="mmsto" />
</intent-filter>
</activity>
<!-- Service that delivers messages from the phone "quick response" -->
<service android:name=".smsReciever.HeadlessSmsSendService"
android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.RESPOND_VIA_MESSAGE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="sms" />
<data android:scheme="smsto" />
<data android:scheme="mms" />
<data android:scheme="mmsto" />
</intent-filter>
</service>
</application>
Can you tell me How to save receive message ?
To receive, I suggest changing your manifest:
<receiver android:name=".smsReciever.SmsListener"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="2147483647">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
<receiver android:name=".smsReciever.MmsReciever"
android:enabled="true"
android:permission="android.permission.BROADCAST_WAP_PUSH">
<intent-filter android:priority="2147483647">
<action android:name="android.provider.Telephony.WAP_PUSH_RECEIVED" />
<data android:mimeType="application/vnd.wap.mms-message" />
</intent-filter>
</receiver>
In addition, you will need to declare the following permissions:
<uses-permission android:name="android.permission.RECEIVE_MMS"/>
<uses-permission android:name="android.permission.RECEIVE_WAP_PUSH"/>
You shouldn't be doing so much work on the receiver, send it to an intentReceiver instead:
#Override
public void onReceive(Context context, Intent intent) {
Timber.i("Intent received: " + intent.getAction());
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
Bundle bundle = intent.getExtras();
Intent intentServiceIntent = new Intent(context, SMSIntentService.class);
intentServiceIntent.putExtras(bundle);
context.startService(intentServiceIntent);
//send broadcast to networkAvailableReceiver
Intent intentNetworkBroadcastReceiver = new Intent();
intentNetworkBroadcastReceiver.setAction("Youraction.CHECK_NETWORK_CONNECTIVITY");
context.sendBroadcast(intentNetworkBroadcastReceiver);
}
}
Here's what I use to parse out the message:
private fun parseOutMessages(intent : Intent?) {
val msgsAny : Array<Any>
var sender: String?
val msgBody: String
var bundle = Bundle()
if (intent != null) {
bundle = intent.extras as Bundle
}
try {
//parses out the message
val pdus = bundle.get("pdus") as Array<Any>
val msgs = Array<SmsMessage?>(pdus.size, {null})
for (i in msgs.indices) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val format = bundle.getString("format")
msgs[i] = SmsMessage.createFromPdu(pdus[i] as ByteArray, format)
} else {
msgs[i] = SmsMessage.createFromPdu(pdus[i] as ByteArray)
}
// Here you have the sender(phone number)
sender = msgs[i]?.originatingAddress
//is the message long enough to send?
var messageBodyLength = 0
if (msgs[i] != null) {
val message = msgs[i] as SmsMessage
messageBodyLength = message.messageBody.length
}
}
} catch (e: Exception) {
e.printStackTrace()
Timber.e(e)
}
}

SecurityException Permission Denial: telephony.SmsProvider uri content://sms/inbox

my phone is android 5.0 API 22.
I just learn build app Android
I create app fake SMS but crash :(
java.lang.SecurityException: Permission Denial: writing com.android.providers.telephony.SmsProvider uri content://sms/outbox from pid=23774, uid=10308 requires android.permission.WRITE_SMS, or grantUriPermission()
My code:
MainActivity
I want test send sms default from phone number 0901123456 and message "test sent" to my phone.
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
public static final String TAG = MainActivity.class.getName();
public static final int PERMISSION_RESULT_CODE = 123;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupView();
setDefaultApp();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{
Manifest.permission.BROADCAST_SMS,
Manifest.permission.READ_SMS,
Manifest.permission.SEND_SMS,
Manifest.permission.RECEIVE_SMS}, PERMISSION_RESULT_CODE);
}
}
}
private void setupView(){
findViewById(R.id.button_draft).setOnClickListener(this);
findViewById(R.id.button_inbox).setOnClickListener(this);
findViewById(R.id.button_outbox).setOnClickListener(this);
findViewById(R.id.button_sent).setOnClickListener(this);
}
private void setDefaultApp(){
final String myPackageName = getPackageName();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (!Telephony.Sms.getDefaultSmsPackage(this).equals(myPackageName)) {
Intent intent =
new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME,
myPackageName);
startActivity(intent);
}
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
if (requestCode == PERMISSION_RESULT_CODE){
for (int i= 0; i< permissions.length; i++) {
Log.d(TAG, permissions[i] + " " + grantResults[i]);
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.button_draft:
ContentValues values1 = new ContentValues();
values1.put("address", "0901123456");
values1.put("body", "test draft");
getContentResolver().insert(Uri.parse("content://sms/draft"), values1);
break;
case R.id.button_inbox:
ContentValues values2 = new ContentValues();
values2.put("address", "0901123456");
values2.put("body", "test inbox");
getContentResolver().insert(Uri.parse("content://sms/inbox"), values2);
break;
case R.id.button_outbox:
ContentValues values3 = new ContentValues();
values3.put("address", "0901123456");
values3.put("body", "test outbox");
getContentResolver().insert(Uri.parse("content://sms/outbox"), values3);
break;
case R.id.button_sent:
ContentValues values4 = new ContentValues();
values4.put("address", "0901123456");
values4.put("body", "test sent");
getContentResolver().insert(Uri.parse("content://sms/sent"), values4);
break;
}
}
}
AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
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>
<!-- BroadcastReceiver that listens for incoming SMS messages -->
<receiver
android:name=".SmsReceiver"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_DELIVER" />
</intent-filter>
</receiver>
<!-- BroadcastReceiver that listens for incoming MMS messages -->
<receiver
android:name=".MmsReceiver"
android:permission="android.permission.BROADCAST_WAP_PUSH">
<intent-filter>
<action android:name="android.provider.Telephony.WAP_PUSH_DELIVER" />
<data android:mimeType="application/vnd.wap.mms-message" />
</intent-filter>
</receiver>
<receiver
android:name=".FinderReceiver"
android:enabled="true"
android:permission="android.permission.RECEIVE_SMS">
<intent-filter>
<action
android:name="android.provider.Telephony.SMS_RECEIVED"
android:priority="999" />
</intent-filter>
</receiver>
<!-- Activity that allows the user to send new SMS/MMS messages -->
<activity
android:name=".ComposeSmsActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.SENDTO" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="sms" />
<data android:scheme="smsto" />
<data android:scheme="mms" />
<data android:scheme="mmsto" />
</intent-filter>
</activity>
<!-- Service that delivers messages from the phone "quick response" -->
<service
android:name=".HeadlessSmsSendService"
android:exported="true"
android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE">
<intent-filter>
<action android:name="android.intent.action.RESPOND_VIA_MESSAGE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="sms" />
<data android:scheme="smsto" />
<data android:scheme="mms" />
<data android:scheme="mmsto" />
</intent-filter>
</service>
</application>
Who can help me? :((
Thank you so much!
You can only write to the SMS provider if your app is the user's chosen SMS client: https://android-developers.googleblog.com/2013/10/getting-your-sms-apps-ready-for-kitkat.html

launch activity after phone restart or manually turn power off or on

how to launch an launch activity after phone restart or manually turn power off or on
i tried below codes but its working for only for unlocking phone but i also want to make it done for boot up manually or restart phone.
below is code
application.class
public class MyApplication extends Application {
public static SharedPreferences preferences;
public static SharedPreferences.Editor editor;
public static String PASSWORD = "password";
public static String IS_SET = "nothing";
public static String MYPREF = "mypref";
public String TAG = getClass().getName();
#Override
protected void attachBaseContext(Context context) {
super.attachBaseContext(context);
MultiDex.install(this);
}
#Override
public void onCreate() {
super.onCreate();
init();
}
public void init() {
preferences = getSharedPreferences(MYPREF, 0);
editor = preferences.edit();
startService(new Intent(getBaseContext(), ScreenReceiver.class));
}
}
ScreenReceiver.class
public class ScreenReceiver extends BroadcastReceiver {
SharedPreferences preferences;
#Override
public void onReceive(Context context, Intent intent) {
System.out.println(intent.getAction());
preferences = context.getSharedPreferences(MYPREF, 0);
if (preferences != null) {
String lock = preferences.getString(IS_SET, null);
if (lock != null) {
if (lock.equals("passcode")) {
gotopasslock(context, intent);
} else {
gotopatternlock(context, intent);
}
}
}
}
public void gotopasslock(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
Intent intent1 = new Intent(context, Main.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent1);
} else if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent myStarterIntent = new Intent(context, Main.class);
myStarterIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myStarterIntent);
}
}
public void gotopatternlock(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
Intent intent1 = new Intent(context, PatternLock.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent1);
} else if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent myStarterIntent = new Intent(context, PatternLock.class);
myStarterIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myStarterIntent);
}
}
}
Main.class
public class Main extends AppCompatActivity {
private PinLockView mPinLockView;
private IndicatorDots mIndicatorDots;
private String TAG = getClass().getName();
String oldPassword = "";
String newPassword = "";
SharedPreferences preferences;
SharedPreferences.Editor editor;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// getSupportActionBar().hide();
super.onCreate(savedInstanceState);
setContentView(R.layout.main_xml);
menifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="">
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme"
tools:replace="label">
<meta-data
android:name="com.google.android.gms.vision.DEPENDENCIES"
android:value="face" />
<activity android:name=".activities.ChooseActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<!-- <category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />-->
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".activities.Home" />
<activity android:name=".activities.MainActivity" />
<activity
android:name=".activities.Main"
android:showOnLockScreen="true" />
<activity
android:name=".activities.PatternLock"
android:showOnLockScreen="true" />
<!--<receiver
android:name=".receiver.ScreenReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
</receiver>-->
<receiver
android:name=".receiver.ScreenReceiver"
android:label="ScreenReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
<intent-filter>
<category android:name="android.intent.category.HOME" />
<action android:name="android.intent.action.SCREEN_ON" />
<action android:name="android.intent.action.SCREEN_OFF" />
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
</manifest>
Intent Action is missing.
To make a android activity launch after reboot, add following line in
AndroidManifest.xml
STEP 1
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Step 2:
<receiver
android:name=".receiver.ScreenReceiver"
android:label="ScreenReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT"
</intent-filter>
</receiver>
NOTE:
try to do code cleanup

Android Manifest code for Parse Push Receiver

I have followed the guidelines mentioned in parse.com and tried all helps from stackoverflow. But I am unable to receive any Parse Push Notification. The Dashboard shows Pushes Sent 0. Can anyone help me out on this. The code for
manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.phpand"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="14" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission
android:name="com.phpand.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.phpand.permission.C2D_MESSAGE" />
<application
android:name=".MainApp"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light" >
<activity android:name=".ParseMainActivity" >
<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.ParsePushBroadcastReceiver"
android:exported="false" >
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
<receiver
android:name="com.parse.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.phpand" />
</intent-filter>
</receiver>
<receiver
android:name=".MyCustomReceiver"
android:exported="false" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
</application>
</manifest>
where MainAppp initializes the Parse, ParseMainActivity's button click sends the push message and MyCustomReceiver is the BroadCastReceiver Class.
MyCustomReceiver class has:
public class MyCustomReceiver extends BroadcastReceiver {
private static final String TAG = "MyCustomReceiver";
public static final String intentAction = "SEND_PUSH";
#Override
public void onReceive(Context context, Intent intent) {
if (intent == null) {
Log.d(TAG, "Receiver intent null");
} else {
processPush(context, intent);
}
}
private void processPush(Context context, Intent intent) {
String action = intent.getAction();
Log.d(TAG, "got action " + action );
if (action.equals(intentAction))
{
String channel = intent.getExtras().getString("com.parse.Channel");
try {
JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
Log.d(TAG, "got action " + action + " on channel " + channel + " with:");
Iterator<String> itr = json.keys();
while (itr.hasNext()) {
String key = (String) itr.next();
if (key.equals("customdata")) {
launchSomeActivity(context, json.getString(key));
triggerBroadcastToActivity(context);
createNotification(context);
}
Log.d(TAG, "..." + key + " => " + json.getString(key));
}
} catch (JSONException ex) {
Log.d(TAG, "JSON failed!");
}
}
}
public static final int NOTIFICATION_ID = 45;
private void createNotification(Context context) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context).setSmallIcon(
R.drawable.ic_launcher).setContentTitle("Successfully logged in");
NotificationManager mNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(45, mBuilder.build());
}
private void launchSomeActivity(Context context, String datavalue) {
Intent pupInt = new Intent(context, ShowPopUp.class);
pupInt.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
pupInt.putExtra("customdata", datavalue);
context.getApplicationContext().startActivity(pupInt);
}
private void triggerBroadcastToActivity(Context context) {
LocalBroadcastManager.getInstance(context).sendBroadcast(new Intent(intentAction));
}
}
Probably I must be missing something in the Manifest file, any help would save my week's due.

Android: Sending SMS works on Emulator but NOT on real devices

Updated:
I have tried this code on another phone (4.1.2), it works perfectly. But when I tried on the phone using 4.4.2, it does not work, it returns "Radio Off". My app is set as default app for handling message FYI.
Here is my Activity to send SMS:
public class ViewConversation extends Activity {
SQLiteManager sql;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.inbox_conversation);
init();
initListView();
initSendSMS();
}
#Override
public void onBackPressed() {
startActivity(new Intent(this, MainLayout.class));
}
private void init() {
sql = new SQLiteManager(this);
}
private void initListView() {
ViewConversationAdapter adapter = new ViewConversationAdapter(this, getMessages());
ListView lv = (ListView) findViewById(R.id.lv_conversation);
lv.setAdapter(adapter);
}
private String[] getMessages() {
Bundle b = getIntent().getExtras();
String fk_conversation_info = b.getString("fk_conversation_info");
sql.open();
String[] result = sql.getConversationSMSs(fk_conversation_info);
sql.close();
return result;
}
/* ---------- SEND SMS PART ---------- */
private void initSendSMS() {
final EditText et_phone = (EditText) findViewById(R.id.et_addPhoneNum);
final EditText et_message = (EditText) findViewById(R.id.et_messageBody);
Button bt_send = (Button) findViewById(R.id.bt_send);
bt_send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String phoneNumber = et_phone.getText().toString();
String message = et_message.getText().toString();
sendSms(phoneNumber, message);
}
});
}
private void sendSms(String phoneNumber, String message) {
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
new Intent(SENT), PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(DELIVERED), PendingIntent.FLAG_UPDATE_CURRENT);
//---when the SMS has been sent---
registerReceiver(new BroadcastReceiver(){
#Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS sent",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off",
Toast.LENGTH_SHORT).show();
break;
}
ViewConversation.this.unregisterReceiver(this);
}
}, new IntentFilter(SENT));
//---when the SMS has been delivered---
registerReceiver(new BroadcastReceiver(){
#Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
}
Here is my manifest for this Activity:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="vn.hak_developers.spamsms" >
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainLayout"
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=".setting.SettingMethods"
android:label="#string/app_name" >
</activity>
<activity
android:name=".viewsms.ViewBanSim"
android:label="#string/app_name" >
</activity>
<activity
android:name=".viewsms.ViewNhaDat"
android:label="#string/app_name" >
</activity>
<activity
android:name=".viewsms.ViewNganHang"
android:label="#string/app_name" >
</activity>
<activity
android:name=".viewsms.ViewThongBao"
android:label="#string/app_name" >
</activity>
<activity
android:name=".viewsms.ViewNhaHang"
android:label="#string/app_name" >
</activity>
<activity
android:name=".viewsms.ViewThuRac"
android:label="#string/app_name" >
</activity>
<activity
android:name=".viewsms.ViewConversation"
android:label="#string/app_name"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.SENDTO" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="sms" />
<data android:scheme="smsto" />
<data android:scheme="mms" />
<data android:scheme="mmsto" />
</intent-filter>
</activity>
<!-- BroadcastReceiver that listens for incoming SMS messages -->
<receiver android:name=".smsmanager.SmsReceiver"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_DELIVER" />
<action android:name="android.provider.Telephony.SMS_RECEIVER" />
</intent-filter>
</receiver>
<!-- BroadcastReceiver that listens for incoming MMS messages -->
<receiver android:name=".smsmanager.MmsReceiver"
android:permission="android.permission.BROADCAST_WAP_PUSH">
<intent-filter>
<action android:name="android.provider.Telephony.WAP_PUSH_DELIVER" />
<data android:mimeType="application/vnd.wap.mms-message" />
</intent-filter>
</receiver>
<!-- Service that delivers messages from the phone "quick response" -->
<service android:name=".smsmanager.HeadlessSmsSendService"
android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.RESPOND_VIA_MESSAGE"/>
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="sms" />
<data android:scheme="smsto" />
<data android:scheme="mms" />
<data android:scheme="mmsto" />
</intent-filter>
</service>
</application>
</manifest>
It works perfectly when I send SMS between emulators, but when I run the app on my phone, it toasts Radio Off (my phone is not in airplane mode).
Please advise.
I made an app that sent SMS last year, but I did not sent the intent in the method.
I did it like this:
sms.sendTextMessage(Number, null, message, null, null);
Try this, maybe it works.
EDIT
There's no need to use all the code in sendSMS(String phoneNumber, String nessage)
simply do this:
private void sendSMS(String phoneNumber, String message)
{
//PendingIntent pi = PendingIntent.getActivity(this, 0,
// new Intent(this, ViewConversation.class), 0);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, null, null);//edited
Toast.makeText(getBaseContext(), "SMS sent", Toast.LENGTH_SHORT).show();
}
the PendingIntent object (pi) is simply pointing to the same activity (ViewConversation.java), so when the SMS is sent, nothing will happen.
Hope this helps.
i fixed, you cant send if sim in SIM 2...I set sim send, call in SIM 1 is working.

Categories

Resources