thank you on advance
I have Issue and I hope u can help me
I m trying to design a service on android to check the httpclient every 10s and notify the the user. the application working find until I kill the apps.
can anyone help me
this my activity :
`using System;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace Lawyer.Droid
{
[Activity(Label = "Lawyer.Droid",LaunchMode=Android.Content.PM.LaunchMode.SingleTop, Icon = "@drawable/icon", Theme = "@style/MyTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
StartService(new Intent(this, typeof(myservice)));
}
}
}
`
this is my service
using System.Threading;
using System.Threading.Tasks;
using Android.App;
using Android.Content;
using Android.Widget;
namespace Lawyer.Droid
{
[Service(Enabled = true,Exported = true, Name = "lawyer.Droid.myservice")]
class myservice : IntentService
{
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
new Task(() =>
{
while (true)
{
Intent intents = new Intent();
intents.SetAction("com.alr.text");
//intents.PutExtra("MyData", "Data from service");
SendBroadcast(intents);
Toast.MakeText(this, "sufyan", ToastLength.Long);
Thread.Sleep(10000);
}
}).Start();
return StartCommandResult.Sticky ;
}
protected override void OnHandleIntent(Intent intent)
{
}
}
this is my board cast :
using System.Collections.Generic;
using System.Net.Http;
using Android.App;
using Android.Content;
using Newtonsoft.Json;
namespace Lawyer.Droid
{
public class Myreciver
{
//public Myreciver()
[BroadcastReceiver(Exported = true, Label = "SMS Receiver")]
[IntentFilter(new string[] {"com.alr.text"})]
[IntentFilter(new[] { Android.Content.Intent.ActionBootCompleted })]
//[IntentFilter(new string[] { "Android.Content.Intent.ActionBootCompleted" })]
public class SMSReceiver : Android.Content.BroadcastReceiver
{
string content1;
public int d1;
public override void OnReceive(Context context, Intent intent)
{
// Log.Info(Tag, "Intent received: " + intent.Action);
// read the SendBroadcast data
if (intent.Action == "com.alr.text")
{
// check the data count is null
if (Lawyer.EmptyClass.Data_count_1 == "")
{
Lawyer.EmptyClass.Data_count_1 = "0";
}
// call the php code
sendtophp();
if (d1 <= System.Convert.ToInt32(Lawyer.EmptyClass.Data_count_1))
{
}
else {
// Start main Activity (you can replace with whatever Activity you want here)
var intent1 = context.PackageManager.GetLaunchIntentForPackage(context.PackageName);
intent1.AddFlags(ActivityFlags.ClearTop);
var pendingIntent = PendingIntent.GetActivity(context, 0, intent, PendingIntentFlags.UpdateCurrent);
var context1 = Android.App.Application.Context;
var builder = new Android.App.Notification.Builder(context1)
.SetSmallIcon(Resource.Drawable.icon)
.SetContentTitle("My application")
.SetDefaults(NotificationDefaults.Sound)
.SetContentText(Lawyer.EmptyClass.Data_count_1)
.SetOngoing(true);
var not = builder.Build();
NotificationManager notManager = (NotificationManager)context.GetSystemService(Context.NotificationService);
notManager.Notify(1, not);
Lawyer.EmptyClass.Data_count_1 = d1.ToString();
}
}
//read incomming sms
}
public async void sendtophp()
{
var client = new HttpClient();
try
{
var response = await client.GetAsync("url");
if (response.IsSuccessStatusCode)
content1 = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<List<MSG>>(response.Content.ReadAsStringAsync().Result);
d1 = result.Count;
}
catch (System.Exception e)
{
}
}
public class MSG
{
public string date { get; set; }
public string text { get; set; }
public string text1 { get; set; }
public string user { get; set; }
public string password { get; set; }
}
}
}
}