I've been through every post I could find and I still can't get my receiver to react to anything. My XML:
<receiver android:name=".CollageSMSReceiver"
android:enabled="true"
android:exported="true" >
android:permission="android.permission.BROADCAST_SMS">
<intent-filter android:priority="2147483647">
<action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
</intent-filter>
</receiver>
It's a large XML so at the top I have:
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
and my Java:
public class CollageSMSReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("sms","fired up");
Object[] pdus = (Object[]) intent.getExtras().get("pdus");
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdus[0]);
String sender = "";
StringBuilder text = new StringBuilder();
// get sender from first PDU
sender = currentMessage.getOriginatingAddress();
for (int i = 0; i < pdus.length; i++) {
currentMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);
text.append(currentMessage.getDisplayMessageBody());
}
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String message = currentMessage.getDisplayMessageBody();
}
}
Thanks for any assistance you can render.
Ok - here's what fixed it. I'm using Samsung note 4 which comes preset with a Samsung messaging app. I downloaded the Google messaging app and disabled the samsung messaging app and it now works like a charm.
Related
Im trying to launch an app that I've created from another app that Im working on right now. The thing is I've been searching throught the internet and found something but it did not work so Im seeking help in here.
This is what I've done on the app I want to launch from :
On my xaml.cs :
public async void GoToDigDitApp(object sender, EventArgs e)
{
var appname = "digdit://";
var result = await DependencyService.Get<IAppHandler>().LaunchApp(appname);
}
I created an Interface:
public interface IAppHandler
{
Task<bool> LaunchApp(string uri);
}
In the Android project:
[assembly: Dependency(typeof(OpenAppAndroid))]
namespace SupervisingApp.Droid
{
[Activity(Label = "OpenAppAndroid")]
public class OpenAppAndroid : Activity, IAppHandler
{
public Task<bool> LaunchApp(string uri)
{
bool result = false;
try
{
var aUri = Android.Net.Uri.Parse(uri.ToString());
var intent = new Intent(Intent.ActionView, aUri);
Android.App.Application.Context.StartActivity(intent);
result = true;
}
catch (ActivityNotFoundException)
{
result = false;
}
return Task.FromResult(result);
}
}
}
And This is the app I want to launch manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.Tab2" android:installLocation="auto">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="28" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<application android:label="Dig Dit" android:icon="#drawable/ic_launcher">
<activity android:icon="#drawable/ic_launcher" android:label="Dig Dit" android:name="digdit.urlentryclass">
<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="digdit" />
</intent-filter>
</activity>
</application>
</manifest>
For now Im only intressted by the Android part and it doesn't seem to work. I hope you guys can help me out.
You could use PackageManager.GetLaunchIntentForPackage.
Try the code below. com.companyname.app2 is the package name of App2.
Intent intent = PackageManager.GetLaunchIntentForPackage("com.companyname.app2");
StartActivity(intent);
Updated:
Create a interface:
public interface IDpendencyService
{
Task<bool> Launch(string stringUri);
}
Implemention of Android:
public class DependencyImplementation : Activity, IDpendencyService
{
public Task<bool> Launch(string stringUri)
{
Intent intent = Android.App.Application.Context.PackageManager.GetLaunchIntentForPackage(stringUri);
if (intent != null)
{
intent.AddFlags(ActivityFlags.NewTask);
Forms.Context.StartActivity(intent);
return Task.FromResult(true);
}
else
{
return Task.FromResult(true);
}
}
}
Usage of MainPage:
<StackLayout>
<!-- Place new controls here -->
<Label
FontAttributes="Bold"
FontSize="Large"
HorizontalOptions="Center"
Text="Welcome to App1!"
VerticalOptions="CenterAndExpand" />
<Button x:Name="GotoApp2" Text="GotoApp2" Clicked="GotoApp2_Clicked"></Button>
</StackLayout>
private void GotoApp2_Clicked(object sender, EventArgs e)
{
DependencyService.Get<IDpendencyService>().Launch("com.companyname.app2");
}
I have upload on GitHub, you could download from the StartAnotherApp_Xamarin.forms/App1 folder for reference.
https://github.com/WendyZang/Test.git
You can open you app from xamarin.forms
Device.BeginInvokeOnMainThread(() =>
{
Xamarin.Forms.Device.OpenUri(new Uri("digdit://555-1111"));
});
I am using xamarin-bluetooth-le to make BluetoothLE client side on Xamarin.
I searched few of BluetoothLE package (sample code is work fine).
Thus, I have choosed xamarin-bluetooth-le.
In fact, sample code work fine.
However, I am simplify sample code without view, becuse I am not need View and binding is not need.
However xamarin-bluetooth-le does not scan device by the simplify code.
First question:
In sample of xamarin-bluetooth-le, an event named DeviceDiscovered is need assign two times?(one is DeviceListViewModel constructor, the other is scan method).
Second(main) question:
Why xamarin-bluetooth-le does not scan device by below codes?
Environment is
Visual Studio 2019 16.10.0
Xamarin 16.10.000.228
Plugin.BLE 2.1.2
Other newest
Below is code:
BluetoothClient.cs
using Plugin.BLE;
using Plugin.BLE.Abstractions.Contracts;
using Plugin.BLE.Abstractions.EventArgs;
using Plugin.BLE.Abstractions.Extensions;
using System;
using System.Collections.ObjectModel;
using System.Threading;
using System.Threading.Tasks;
namespace PlugInBLETest.NetowrkModels
{
public class BluetoothBLEClient
{
private IAdapter Adapter;
private IBluetoothLE Current;
public ObservableCollection<IDevice> DeviceList { get; }
private CancellationTokenSource CancelSource;
public BluetoothBLEClient()
{
Current = CrossBluetoothLE.Current;
Adapter = CrossBluetoothLE.Current.Adapter;
Adapter.DeviceDiscovered += OnDeviceDiscovered;
Adapter.ScanTimeoutElapsed += OnScanTimeoutElapsed;
Adapter.DeviceDisconnected += OnDeviceDisconnected;
Adapter.DeviceConnectionLost += OnDeviceConnectionLost;
DeviceList = new ObservableCollection<IDevice>();
}
public async Task SearchDevices()
{
if (Current.State == BluetoothState.Off)
{
return;
}
else
{
DeviceList.Clear();
foreach (var connectedDevice in Adapter.ConnectedDevices)
{
try
{
await connectedDevice.UpdateRssiAsync();
}
catch (Exception ex)
{
return;
}
}
CancelSource = new CancellationTokenSource();
Adapter.ScanMode = ScanMode.LowLatency;
Adapter.ScanTimeout = 30000;
//Adapter.DeviceDiscovered += (s, a) => DeviceList.Add(a.Device);
Adapter.DeviceDiscovered += (s, a) =>
{
DeviceList.Add(a.Device);
};
await Adapter.StartScanningForDevicesAsync(CancelSource.Token);
var temp = DeviceList.Count;
}
return;
}
private void OnDeviceConnectionLost(object sender, DeviceErrorEventArgs e)
{
}
private void OnDeviceDisconnected(object sender, DeviceEventArgs e)
{
}
private void OnScanTimeoutElapsed(object sender, EventArgs e)
{
}
private void OnDeviceDiscovered(object sender, DeviceEventArgs args)
{
}
}
}
AndroidManifext.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.pluginbletest">
<uses-sdk android:targetSdkVersion="30" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<application android:label="PlugInBLETest.Android" android:theme="#style/MainTheme"></application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
Xamarin.Essentials has a permissions feature that will allow you to check for and request permission for access to the location data
After Marshmallow you have to prompt for the user for permission -> https://devblogs.microsoft.com/xamarin/requesting-runtime-permissions-in-android-marshmallow/
I am trying to download a file using volley plus(https://github.com/DWorkS/VolleyPlus/blob/master/library/src/com/android/volley/request/DownloadRequest.java) . Here's my code:
#Override
public void downloadFile(String url, final String path, OnViewDocsFinished mListener) {
//url = "http://www.orimi.com/pdf-test.pdf"
//String name = "passport.pdf";
//String path = Environment.DIRECTORY_DOWNLOADS+"/"+name;
DownloadRequest request = new DownloadRequest(url, path,
createSuccessListener(mListener, path),
createErrorListener(mListener));
VolleySingleton.getRequestQueue().add(request);
}
private Response.Listener<String> createSuccessListener(final OnViewDocsFinished loginListener, final String path) {
return new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
Log.i(TAG, response.toString());
loginListener.onDownloadFinished(path);
} catch (Exception e) {
}
}
};
}
private Response.ErrorListener createErrorListener(final OnViewDocsFinished loginListener) {
return new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, error.toString());
}
};
}
The file is not getting downloaded to the location. Here's my manifest file:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="in.yumigo.yumigovendor">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER" />
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
I am running my app on Android Lollipop. WHat's the issue here guys?
You trying to download file i.e.byte[] by making custom string request. If you make string request you will get only string response. In your case you need to make byte[] request.
Check below link how to setup custom request class for volley.
Implementing a Custom Volley Request.
This below link is making the same request what you are trying to make. Hope it may help you.
Downloading files using volley.
I am using GCM push notification service in my android app. Now, I have to Use both parse and GCM push notification in my android app. But, I am not getting notification from both GCM and Parse (may be due to conflict). I can reach both GCM and Parse individually but never together, My implementation looks this way;
I have registered Receivers for both GCM and Parse as fallows;
The following receiver is for GCM;
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<!-- Receives the actual messages. -->
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<!-- Receives the registration id. -->
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.example.test" />
</intent-filter>
</receiver>
The receiver and service for Parse is as fallows;
<service android:name="com.parse.PushService"></service>
<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 android:priority="0">
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.example.test" />
</intent-filter>
</receiver>
I have called Parse Initialization in Application class as;
Parse.setLogLevel(Parse.LOG_LEVEL_VERBOSE);
Parse.initialize(this, "APPLICATION_ID", "CLIENT_KEY");
ParseInstallation.getCurrentInstallation().saveInBackground();
I have implemented GCMIntentService for GCM as fallows;
public class GCMIntentService extends GCMBaseIntentService {
private static final String TAG = "GCMIntentService";
GCMManager gcmManager;
public GCMIntentService() {
super(Config.SENDER_ID);
}
/**
* Method called on device registered
*/
#Override
protected void onRegistered(Context context, String registrationId) {
gcmManager = new GCMManager(context);
Log.i(TAG, "Device registered: regId = " + registrationId);
displayMessage(context, "Your device registred with GCM");
ServerUtilities.register(context, registrationId);
gcmManager.saveGcmRegistrationId(registrationId);
}
/**
* Method called on device un registred
*/
#Override
protected void onUnregistered(Context context, String registrationId) {
Log.i(TAG, "Device unregistered");
displayMessage(context, getString(R.string.gcm_unregistered));
ServerUtilities.unregister(context, registrationId);
}
/**
* Method called on Receiving a new message
*/
#Override
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Received message");
// displayMessage(context, message);
try {
String message = intent.getExtras().getString(Constant.TAG_MESSAGE);
String id = intent.getExtras().getString(Constant.TAG_ID);
String title = intent.getExtras().getString(Constant.TAG_TITLE);
String type = intent.getExtras().getString(Constant.TAG_NOTIFICATION_TYPE);
Log.e("Message : ", message);
Log.e("Title : ", title);
Log.e("ID : ", id);
Log.e("Type : ", type);
// notifies user
generateNotification(context, message, title, id, type);
} catch (Exception e) {
}
}
}
The whole problem i have been facing is with this line of code
PushService.setDefaultPushCallback(this, MainActivity.class);
on importing PushService the setDefaultPushCallback|() got deprecated. Why is this happening. I receiving the notifications but on tap app is being crashed. Also not receiving when the app isn't running.
I have found the solution and it is quite simple.
I found the same question
https://stackoverflow.com/a/26180181/3904085
"
After spending few hours. Found a solution: Implement your receiver and extends ParsePushBroadcastReceiver class.
Receiver.java
public class Receiver extends ParsePushBroadcastReceiver {
#Override
public void onPushOpen(Context context, Intent intent) {
Log.e("Push", "Clicked");
Intent i = new Intent(context, HomeActivity.class);
i.putExtras(intent.getExtras());
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
Use it in manifest, (Instead of using ParsePushBroadcastReceiver)
Code for project's manifest:
<receiver
android:name="your.package.name.Receiver"
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>
"
Credits to #Ahmad Raza
From Parse documentation on onPushOpen():
Called when the push notification is opened by the user. Sends analytics info back to Parse that the application was opened from this push notification. By default, this will navigate to the Activity returned by ParsePushBroadcastReceiver.getActivity(Context, Intent). If the push contains a 'uri' parameter, an Intent is fired to view that URI with the Activity returned by ParsePushBroadcastReceiver.getActivity(android.content.Context, android.content.Intent) in the back stack.
So if you override onPushOpen() like that, no analytics will be sent.
So here is my code:
public class Receiver extends ParsePushBroadcastReceiver {
#Override
protected Class<? extends Activity> getActivity(Context context, Intent intent) {
return HomeActivity.class;
}
}
You need to register the receiver like in the above post.
Tested with Parse 1.10.3