BroadcastReceiver for android os return nothing - android

This is little snipet to try BroadcastReciever android but i dont understand wath's wrong.
In fact just callIntent return log. my little brain it's burning.
if somebody can tell me why ?
Sorry for my bad english !
```
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.plectre.broadcast">
<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=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MyReciever" >
<intent-filter>
<action android:name="com.plectre"/>
</intent-filter>
</receiver>
</application>
</manifest>
```
public void callIntent() {
Intent intent = new Intent();
intent.setAction("com.plectre");
intent.putExtra("frist", "Intent premier" );
sendBroadcast(intent);
Log.i("callIntent", "Tag");
}
```
public class MyReciever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//String result = intent.getStringExtra("first");
Toast.makeText(context, "Result", Toast.LENGTH_LONG).show();
Log.i("Broadcast", " Receiver");
}
```
No error, no crash, nothing !! :=(

Create an explicit intent by explicitly declaring the receiver when you create the intent. At present your intent is implicit.
public void callIntent() {
// Explicitly declare the receiver class
Intent intent = new Intent(this, MyReciever.class);
intent.setAction("com.plectre");
intent.putExtra("frist", "Intent premier" );
sendBroadcast(intent);
Log.i("callIntent", "Tag");
}
Additional reading: https://developer.android.com/guide/components/intents-filters#ExampleExplicit
Also. You declare the intent's Extra as "frist" and try to receive at as "first". This needs to be rectified.
You should ensure that the correct action is taken with the specified Intent Action in your receiver:
#Override
public void onReceive(Context context, Intent intent) {
if ("com.plectre".equals(intent.getAction())) {
final String result = intent.getStringExtra("first");
Toast.makeText(context, "Result", Toast.LENGTH_LONG).show();
Log.i("Broadcast", " Receiver");
}
}

Related

Boot Complete Broadcast not Working

I am trying to start an activity when am restart my phone then its open app or show me toast when booting is complete
class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
Intent serviceIntent = new Intent(context, MyIntentService.class);
context.startService(serviceIntent);
}
}
}
this is my Broadcaste receiver Code
class MyIntentService extends Service {
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
// do something when the service is created
}
}
This is my service Code.
Manifest
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".MyIntentService"></service>
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
am trying lots of diffrent code but no one work for me so can anyone help me to correct this code
Your BroadcastReceiver will never get called because you have this in the manifest entry for it:
android:exported="false"
Remove that.
NOTE: You also need to make sure that your app is started at least once manually after installing it on the phone. Otherwise your BroadcastReceiver will NOT get the BOOT_COMPLETE Intent.
NOTE: Also, using Toast as a debugging aid isn't a very good idea. You should write messages to the logcat and use that to determine if your Service is getting started, etc. Toast is not reliable as a debugging tool.
Add this in BroadcastReceiver class
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent pushIntent = new Intent(context, SyncData.class);
context.startService(pushIntent);
Log.e("BroadCast Received", "ON BOOT COMPLETE");
}
}
and remove this two lines android:enabled="true"
android:exported="false"

My Broadcast Receiver doesn't receive for intents

I'm developing an app and i need another external application with a broadcast receiver only. Here is my code:
app1:
Intent intent = new Intent();
intent.setAction("com.blabla.myaction");
intent.putExtra("extra", "test");
sendBroadcast(intent);
app2 (The one with the receiver):
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.test" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<receiver
android:name=".myReceiver">
<intent-filter>
<action android:name="com.blabla.myaction" />
</intent-filter>
</receiver>
</manifest>
public class myReceiver extends BroadcastReceiver {
private Context mContext;
public static final String ACTION = "com.blabla.myaction";
#Override
public void onReceive(Context context, Intent intent) {
mContext = context;
if (ACTION.equals(intent.getAction())) {
Log.e("lala", "received");
String extra = intent.getStringExtra("extra");
if (packageName != null) {
Log.e("lala", extra);
}
}
}
With this, i doesn't get the "received" log nor the extra. Why?
You "recieving application has to have been started at least once.
You may want to review https://developer.android.com/reference/android/content/BroadcastReceiver.html.

Unable to start service Intent: not found

I've the same issue described here but i can't understand whats wrong.
My issue is: Unable to start service Intent { act=.connections.MoodyService } U=0: not found
EDIT
I've changed my package from connections to service in the source code, sorry for the confusion
My manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.moody"
android:installLocation="auto"
android:versionCode="0"
android:versionName="0.6.7 alpha" >
<uses-sdk
android:maxSdkVersion="18"
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:allowClearUserData="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="activities.MainActivity"
android:label="#string/app_name" >
</activity>
<activity
android:name="activities.Menu_esq"
android:label="#string/title_activity_menu_esq" >
</activity>
<activity
android:name="activities.BaseActivity"
android:label="#string/title_activity_base" >
</activity>
<activity
android:name="activities.MainView"
android:label="#string/title_activity_main_view" >
</activity>
<activity
android:name="activities.LoginActivity"
android:label="#string/app_name"
android:noHistory="true"
android:windowSoftInputMode="adjustResize|stateVisible" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.moody.LeftActivity"
android:label="#string/title_activity_left" >
</activity>
<activity
android:name="com.example.moody.RightActivity"
android:label="#string/title_activity_right" >
</activity>
<activity
android:name="activities.UserDetailsActivity"
android:label="#string/title_activity_user_details" >
</activity>
<activity
android:name="fragments.TopicsPreview"
android:label="#string/title_activity_copy_of_topics_preview" >
</activity>
<activity android:name="activities.Loading" >
</activity>
<service
android:name=".service.MoodyService"
android:icon="#drawable/ic_launcher"
android:label="#string/moody_service" >
</service>
</application>
service is the package and MoodyService is the class name
My service class
public class MoodyService extends Service {
public MoodyService() {
// TODO Auto-generated constructor stub
}
private boolean isRunning = false;
Object getContent;
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onCreate() {
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
// Announcement about starting
Toast.makeText(this, "Starting the Demo Service", Toast.LENGTH_SHORT)
.show();
// Start a Background thread
isRunning = true;
Thread backgroundThread = new Thread(new BackgroundThread());
backgroundThread.start();
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
// Stop the Background thread
isRunning = false;
// Announcement about stopping
Toast.makeText(this, "Stopping the Demo Service", Toast.LENGTH_SHORT)
.show();
}
private class BackgroundThread implements Runnable {
int counter = 0;
public void run() {
try {
counter = 0;
while (isRunning) {
System.out.println("" + counter++);
new Contents().getAll(getResources(),
getApplicationContext());
Thread.currentThread().sleep(5000);
}
System.out.println("Background Thread is finished.........");
} catch (Exception e) {
e.printStackTrace();
}
}
}
And in my main Intent.
Intent start = new Intent(".service.MoodyService");
this.startService(start);
and also tried
Intent intent = new Intent(this, MoodyService.class);
this.startService(intent);
and tried with the full path
<service
android:name="com.example.moody.service.MoodyService"
android:icon="#drawable/ic_launcher"
android:label="#string/moody_service" >
Solved
I deleted the period in the beginning of the package name in the manifest and it worked, in another words:
This doesn't work:
.yourPackage.YourClass
But this does work:
yourPackage.YourClass
And in the main:
Intent intent = new Intent(this, MoodyService.class);
this.startService(intent);
But it goes against what is written in the documentation:
android:name The name of the Service subclass that implements the service. This should be a fully qualified class name (such as,
"com.example.project.RoomService"). However, as a shorthand, if the
first character of the name is a period (for example, ".RoomService"),
it is appended to the package name specified in the
element. Once you publish your application, you should not change this
name (unless you've set android:exported="false").
There is no default. The name must be specified.
The service must be also be included in the Manifest:
<service android:name="com.x.x.serviceclass"></service>
Make sure you have declared your service in the manifest file.
<service
android:name=".MyService"
android:enabled="true" >
</service>
and try writing getApplicationContext() instead of "this" keyword
startService(new Intent(getApplicationContext(), MoodyService.class));
I don't know why you are using that package-like name for your service name, but why don't you use class name for starting the service?
Intent intent = new Intent(context, YourService.class);
context.startService(intent);
I think in manifest package name for service is wrong as you said your package name is connections so it should be like this
android:name ="connections.MoodyService"
or
android:name="com.example.moody.connections.MoodyService"
to invoke service do
Intent intent = new Intent(this, MoodyService.class);
this.startService(intent);
my service is in "service" package and my manifest service enrty like this;
<service
android:name="service.TimerService"
android:exported="false" >
</service>
Did you create an empty constructor in the service?
If not, try that.
Also uncertain if you can use the Intent like that. You could try the 'new Inten(this, MoodyService.class)' construction.
Did you try to use the android:name that you specified in the Manifest?
Android Manifest:
<service
android:enabled="true"
android:name="UploadService" />
The call would be sth like this:
Intent intent = new Intent("UploadService");

Android: DeviceAdmin Application: Unable to start activity to add device admin

I was following the example code for the Device Admin app. My attempts to start activity that enables the user to add a device admin app has failed. The following is the code which I use to try and start this.
public class DeviceAdminTestActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ComponentName deviceAdminReceiver = new ComponentName(this, TestAdminReceiver.class);
Intent i = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
i.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, deviceAdminReceiver);
i.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "My Explanantion");
// startActivityForResult(i, 1);
try{
startActivity(i);
}catch(Exception ex){
Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG);
}
}
}
The following is the code of the Receiver...
public class TestAdminReceiver extends DeviceAdminReceiver {
#Override
public void onEnabled(Context context, Intent intent) {
Toast.makeText(context, "On Enabled", Toast.LENGTH_SHORT).show();
}
#Override
public CharSequence onDisableRequested(Context context, Intent intent) {
return ("You are about to disable device admin");
}
#Override
public void onDisabled(Context context, Intent intent) {
Toast.makeText(context, "On Disabled", Toast.LENGTH_SHORT).show();
}
#Override
public void onPasswordChanged(Context context, Intent intent) {
Toast.makeText(context, "On Password Changed", Toast.LENGTH_SHORT).show();
}
}
And finally, here is the manifest...
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.zeezulander.admintest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name="org.zeezulander.admintest.DeviceAdminTestActivity"
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=".TestAdminReceiver" android:permission="android.permission.BIND_DEVICE_ADMIN" android:label="Test Admin">
<meta-data android:resource="#xml/admin_data" android:name="adminMetaData"/>
<intent-filter >
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED"/>
</intent-filter>
</receiver>
</application>
I do not get an error even. My Activity starts, but nothing happens beyond, no error even.
Maybe something is wrong with the layout. If you're not getting an error, then it isn't crashing, and if it isn't crashing then the only thing that could be wrong is the display (layout).
final Intent intent = new Intent();
intent.setComponent(new ComponentName("com.android.settings", "com.android.settings.DeviceAdminAdd"));
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, adminReceiver);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "To use all features activate it.");
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
startActivityForResult(intent, ACTIVATION_REQUEST);

Error when trying initialize a service ( which extends IntentService class)

I try to play with Service in Android, my application contain an Activity with a button so that it can do action when I press it.
The core component is the ExtractingURLService , which extends IntentService framework class.
I have overrided the onHandleIntent() method, so that it can handle intent passed to it properly.
Code:
public class ExtractingURLService extends IntentService {
private static final String TAG = "ExtractingURLService";
public ExtractingURLService(String name) {
super("ExtractingURLService");
}
#Override
protected void onHandleIntent(Intent intent) {
// do something here !
}
}
}
The Android Manifest file is :
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="example.service.urlextraction"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET"/>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".ServiceExample02Activity"
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=".ExtractingURLService" android:enabled="true"></service>
</application>
</manifest>
In my only Activity class, I call the service by using:
Intent startServiceIntent = new Intent(this, ExtractingURLService.class);
startService(startServiceIntent);
That's all my work, but when deploy, the runtime error I received was:
04-09 16:24:05.751: ERROR/AndroidRuntime(1078): Caused by:
java.lang.InstantiationException:
example.service.urlextraction.ExtractingURLService
What I should do with that ??
Add full path of Service
<service android:name="com.foo.services.ExtractingURLService" android:enabled="true"></service>
Intent Service should look like this:
package com.foo.services;
import android.app.IntentService;
import android.content.Intent;
public class ExtractingURLService extends IntentService {
private static final String TAG = "ExtractingURLService";
public ExtractingURLService() {
super("ExtractingURLService");
}
#Override
protected void onHandleIntent(Intent intent) {
// do something here !
}
}
You can add try and catch block to avoid crash.Its work for me
try {
Intent intent = new Intent(DashboardActivity.this, MyService.class);
startService(intent);
}catch (IllegalStateException e){
}catch (Exception ee){
}

Categories

Resources