I'm getting this error in log cat when i receive a notification but not always. Seems that sometimes the String i want pass and display in a toast results null but not always. The error is this as from title:
java.lang.RuntimeException: Error receiving broadcast Intent { act=it.dd.notification flg=0x10 (has extras) }
--
--
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x7f04006b
--
at it.dd.notification.Main$2.onReceive(Main.java:417)
--
i can't post all logCat because i don't have Eclipse here. I remember this parts that are what i need to understand the problem.
I declared these first in MainActivity:
//private String dir;
protected MyReceiver mReceiver = new MyReceiver();
public static String INTENT_ACTION_NOTIFICATION = "it.dd.notification";
private String titoloNoti;
private String contenutoNoti;
//
Always in MainActivity i have this BroadcastReceiver:
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent != null) {
Bundle extras = intent.getExtras();
String notificationTitle = extras.getString(Notification.EXTRA_TITLE);
CharSequence notificationText = extras.getCharSequence(Notification.EXTRA_TEXT);
contenutoNoti = notificationText.toString(); //Line 417
Toast loc = Toast.makeText(getApplicationContext(), contenutoNoti, Toast.LENGTH_SHORT);
loc.show();
}
}
}
So when i intercept a notification it crashes. But, i repeat, not always. Sometimes when it wants and it's so frustrating!! Have you any ideas?
Related
Please be informed, we are trying to start activity from service class, which is fired on clicking push notification addaction intent. The service class contains two actions, one to stop playing ringtone and another to startactivity. But unfortunately the start activity just does not boot in our service class.
Service Class page is as given below:
public class RingtonePlayingService extends Service {
private static final String TAG = RingtonePlayingService.class.getSimpleName();
private static final String URI_BASE = RingtonePlayingService.class.getName() + ".";
public static final String ACTION_DISMISS = URI_BASE + "ACTION_DISMISS";
public static final String ACTION_START = URI_BASE + "ACTION_START";
private MediaPlayer mp;
private Ringtone ringtone;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand");
int notificationId = intent.getIntExtra("notificationId", 0);
if (intent == null) {
Log.d(TAG, "The intent is null.");
return START_REDELIVER_INTENT;
}
String action = intent.getAction();
if (ACTION_START.equals(action)) {
String uri = String.valueOf(intent.getIntExtra("uri", 0));
Intent intents = new Intent(this, MainActivity.class);
intents.putExtra("uri", uri);
intents.putExtra("notification_id", notificationId);
intents.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
this.startActivity(intents);
}
return START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
mp.reset ();
}}
Firebase (from where service class's intent is fired)
Intent startIntent = new Intent(this, RingtonePlayingService.class);
startIntent.setAction(RingtonePlayingService.ACTION_START);
startIntent.putExtra("uri", uri);
startIntent.putExtra("notification_id", notification_id);
PendingIntent pt = PendingIntent.getService(this,123, startIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Action action_n = new NotificationCompat.Action.Builder(R.mipmap.ic_launcher, tag, pt).build();
The objective here is on firing of pending Intent 'pt'. We want to start the activity (that opens the app's url) as well as run the service class (which stops the service and the ringtone).
Please help us find a solution on this never ending issue.
The service class contains two actions, one to stop playing ringtone and another to startactivity. But unfortunately the start activity just does not boot in our service class.
You cannot start an activity from the background on modern versions of Android.
Firebase (from where service class's intent is fired)
That code has problems:
Your Intent is for RingtonePlayingService, which according to your first code snippet is a Service. Yet, you try using it with PendingIntent.getActivity(), rather than PendingIntent.getService().
You are using addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK), which are irrelevant for a Service.
My code is working perfectly, i used a Brodcast Receiver.
In my application when the user click a button, a phone call is started, and the second activity (outcall.java) popup the screen, in the outcall Activity i have a VideoView, what I want to do is that the Video starts when the call is answered, and the activity is killed when the call is ended.
I have another problem with my code, i want the second activity to launch only when I use the button inside the application, because now it launches always even if i call a friend.
help will be appreciated
Here is my MainActivity
public class MainActivity extends AppCompatActivity {
Button play;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
registerReceiver(broadcastReceiver, new IntentFilter(""));
play = (Button)findViewById(R.id.button2);
final MediaPlayer mP=MediaPlayer.create(MainActivity.this,R.raw.reco);
play.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
if (mP.isPlaying()){
mP.pause();
play.setBackgroundResource(R.drawable.play);
}else {
mP.start();
play.setBackgroundResource(R.drawable.pause);
}
}
});
ActivityCompat.requestPermissions(this, new String[]{CALL_PHONE}, PackageManager.PERMISSION_GRANTED);
}
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
OpenCallOut();
}
};
public void OpenCallOut(){
Intent intent = new Intent(this, Outcall.class);
startActivity(intent);
}
public void CallButton(View view) {
if (checkSelfPermission(Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
return;
}
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "123456"));
startActivity(intent);
}
#Override
public void onDestroy() {
finish();
System.exit(0);
}
}
Here is my Broadcast, i found this in codetoart but don't know how to use it
public class Broadcast extends BroadcastReceiver {
private static final String TAG = "PhoneStateBroadcastReceiver";
Context mContext;
String incoming_nr;
private int prev_state;
#Override
public void onReceive(Context context, Intent intent) {
context.sendBroadcast(new Intent(""));
TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
CustomPhoneStateListener customPhoneListener = new CustomPhoneStateListener();
telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
Bundle bundle = intent.getExtras();
String phoneNr= bundle.getString("incoming_number");
Log.v(TAG, "phoneNr: "+phoneNr);
mContext=context;
}
public class CustomPhoneStateListener extends PhoneStateListener {
private static final String TAG = "CustomPhoneStateListener";
#Override
public void onCallStateChanged(int state, String incomingNumber){
if(incomingNumber!=null&&incomingNumber.length()>0) incoming_nr=incomingNumber;
switch(state){
case TelephonyManager.CALL_STATE_RINGING:
Log.d(TAG, "CALL_STATE_RINGING");
prev_state=state;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d(TAG, "CALL_STATE_OFFHOOK");
prev_state=state;
break;
case TelephonyManager.CALL_STATE_IDLE:
Log.d(TAG, "CALL_STATE_IDLE==>"+incoming_nr);
if((prev_state==TelephonyManager.CALL_STATE_OFFHOOK)){
prev_state=state;
}
if((prev_state==TelephonyManager.CALL_STATE_RINGING)){
prev_state=state;
}
break;
}
}
}
}
Here is my second activity Outcall.Java
public class Outcall1 extends AppCompatActivity {
VideoView myVideo;
private MediaController media_control;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_outcall1);
myVideo = (VideoView) findViewById(R.id.videoView);
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.myvideo);
media_control = new MediaController(this);
myVideo.setMediaController(media_control);
myVideo.setVideoURI(uri);
myVideo.start();
}
}
There are basically multiple ways for sending broadcasts in android.
In simple terms, some are due to system events such as the all the outgoing calls, while some are private to a particular application i.e the local broadcasts within the app.
1.In your case, the second activity is launching always because your onRecieve() is responding to system events. Hence you should try to sending local broadcast on the button click using the LocalBroadcastManager.sendBroadcast.
Alternatively , you can also use Service for starting your second activity .In this case , before starting call intent start your service .Inside the servie you can set a small delay and then start the second activity.
And for finishing your second activity on call end , you can use the above broadcast reciever that you posted but know for detecting call end, and then in case when the state of phone is call disconnected. Fire another broadcast to be recieved by the second activity which finishes the activity on recieving it.
This will help you with the implementation of the step 2.
How to get the value that i put in Broadcast Receiver?
Please help me
In my Activity
public static void startActivity(Context context) {
//what can i put here?
}
In Intent Service
intent.putExtra("DriversID", 1351351);
sendBroadcast(intent);
my Broadcast Reciver
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
String DriversID = null;
if (bundle != null) {
DriversID = bundle.getString("DriversID");
Toast.makeText(context, "DriversID", Toast.LENGTH_LONG).show();
}
Intent i = new Intent(context, DashboardActivity.class);
i.putExtra("getDriversID", DriversID);
context.startActivity(i);
ComponentName comp = new ComponentName(context.getPackageName(),
GcmIntentService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
here is the exact scenario , when the GCM notify me i will get the message from inten and post it to broadcast and then activity will get the value of message , how about that ?
You can create a BroadcastReciever in your activity:
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// Extract data included in the Intent
// String message = intent.getStringExtra("message");
// do other stuff here
}
};
You then should register in your activiy (in onResume for example):
this.registerReceiver(mMessageReceiver, new IntentFilter("unique_name"));
And remember to unregiester (here in onPause, choose whatever fits your needs):
protected void onPause() {
super.onPause();
this.unregisterReceiver(mMessageReceiver);
}
To send the data you can use this:
Intent intent = new Intent("unique_name");
// put whatever data you want to send, if any
// intent.putExtra("message", "some data");
// send broadcast
context.sendBroadcast(intent);
}
This will do.
Set an action for intent and send broadcast.
intent.setAction(action);
and check for the action in receiver to find the intent
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(action))
String DriversID = intent.getStringExtra("DriversID");
}
I have created a service which launch Activity after receiving SMS from certain numbers.
There are 2 Activities Success and Failure.
Both Activities opens successfully only 1 time, On second iteration, when i send SMS again, it does not effect Activity Screen.
It remains open the current Activity screen and does not change it based on the condition.
I searched this on net and found various solution, but nothing seems working here, I tried to change flags, but it only allows 1 flag from service class, and if i choose other Flags it gives me following error message.
StartActivity from outsite an activity context requires the FLAG_ACTIVITY_NEW_TASK flag
This is the service class i have written. Kindly check this and guide me what i am doing wrong here.
Thanks
public class IncomingSms extends BroadcastReceiver {
final SmsManager sms = SmsManager.getDefault();
int duration = Toast.LENGTH_LONG;
#Override
public void onReceive(Context context, Intent intent) {
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String senderNum = phoneNumber;
String message = currentMessage.getDisplayMessageBody();
if(senderNum.equals("345"))
{
Intent successScreen= new Intent(context, SuccessActivity.class);
successScreen.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(successScreen);
}
else if(senderNum.equals("3450") || senderNum.equals("3451") || senderNum.equals("3452")){
Intent alertActivity = new Intent(context, FailureActivity.class);
alertActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(alertActivity);
}
Try to replace your code with this code:
if(senderNum.equals("345"))
{
Intent successScreen= new Intent(context, SuccessActivity.class);
successScreen.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
successScreen.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
successScreen.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
context.startActivity(successScreen);
}
else if(senderNum.equals("3450") || senderNum.equals("3451") || senderNum.equals("3452")){
Intent alertActivity = new Intent(context, FailureActivity.class);
alertActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
successScreen.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
successScreen.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
context.startActivity(alertActivity);
}
I have a mUsbReceiver (BroadcastReceiver) and CameraActivity. The receiver setContentView(R.layout.main) from CameraActivity via an Intent. Then CamearActivity updates its View with this value. Notice that the setContentView is in the Broadcast receiver class and not in the CameraActivity Class.
public class CameraActivity extends Activity {
private static final String TAG = "openXC::Activity";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
usbConnection();
}
public void usbConnection() {
UsbManager mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
PendingIntent mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_CANCEL_CURRENT);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
registerReceiver(mUsbReceiver, filter);
String txt = "default";
HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();
Log.i(TAG, "Device List: " + deviceList);
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
UsbDevice device = deviceIterator.next();
Log.i(TAG, "Device List: " + deviceList);
mUsbManager.requestPermission(device, mPermissionIntent);
}
private static final String ACTION_USB_PERMISSION ="com.ford.openxc.webcam.USB_PERMISSION";
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION_USB_PERMISSION.equals(action)) {
synchronized (this) {
UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
if(device != null){
Log.d(TAG, "Displayed Comten View " + device);
setContentView(R.layout.main);
}
}
else {
Log.d(TAG, "permission denied for device " + device);
}
}
}
}
};
}
This works fine sometimes but sometimes throws the following error
I/openXC::Activity( 5609): Device List: {/dev/bus/usb/001/002=UsbDevice[mName=/dev/bus/usb/001/002,mVendorId=1133,mProductId=2085,mClass=239,mSubclass=2,mProtocol=1,mInterfaces=[Landroid.os.Parcelable;#421a1f50]}
I/openXC::Activity( 5609): Device List: {/dev/bus/usb/001/002=UsbDevice[mName=/dev/bus/usb/001/002,mVendorId=1133,mProductId=2085,mClass=239,mSubclass=2,mProtocol=1,mInterfaces=[Landroid.os.Parcelable;#421a1f50]}
I/Adreno200-EGLSUB( 5609): <ConfigWindowMatch:2087>: Format RGBA_8888.
E/ ( 5609): <s3dReadConfigFile:75>: Can't open file for reading
E/ ( 5609): <s3dReadConfigFile:75>: Can't open file for reading
D/openXC::Activity( 5609): Displayed Comten View UsbDevice[mName=/dev/bus/usb/001/002,mVendorId=1133,mProductId=2085,mClass=239,mSubclass=2,mProtocol=1,mInterfaces=[Landroid.os.Parcelable;#421d3ed0]
D/WebcamPreview( 5609): WebcamPreview constructed
Technically, you can call setContentView any time you are executing on the event thread.
Otherwise you need to use a Handler to call it.
Also, here are some usefull links that might help you:
link1
link 2
link 3
I dont have much exp on USB sort of thing but since u said its saying cannot readfile.. i believe dat the error may be in the usb so for the debugging purpose i would suggest to move the setContentView(int) from if conditions to the onRecieve directly so dat whenever the onReceive is called ur contenview will change , this will help to ensure that the error is not with setcontentview... After dat u can see without setcontentview in the usb and now if the error is coming then surely the error is in the usb and not in the setContentView ....
Hope it works :)