So here is the setup.. I have two apps AppReceiver.apk and AppClient.apk. AppReceiver project has just one receiver and no other activity and AppClient just sends the broadcast message. This works in my emulator with Android 4.0 api level 14 on my mac but not in windows or on actual device.
Code for AppReceiver.apk
*MyReceiver.java*
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context arg0, Intent arg1) {
Log.d("MyReceiver", "Got Message");
}
}
Manifest file
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.appreceiver"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<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.appreceiver.CUSTOM_INTENT"></action>
</intent-filter>
</receiver>
</application>
</manifest>
Code for AppClient
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sendButton = (Button)findViewById(R.id.sendbroadcastButton);
sendButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v)
{
Intent intent = new Intent();
intent.setAction("com.appreceiver.CUSTOM_INTENT");
sendBroadcast(intent);
}
});
}
Manifest file
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.appclient"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.appclient.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
So here is what is happening when I click on the sendbroadcast button in my emulator that is running in Mac then I see the log message Log.d("MyReceiver", "Got Message"); in my logcat meaning it is working as expected. I can click more than once and it works as expected.
However when I installed it on the device I do not see the message in logcat (I made sure I am reading the logcat from device) Also I have another windows laptop and on that one it won't show this message on the emulator either. So I am not sure what is going on? Any pointers?
So I confirmed that I will have to start AppReceiver.apk at least once after install and this is a new requirement or safety thing after Android 3.1 as they want user to explicitly start an app in order to user its broadcast receiver.
Since I did not wanted any GUI thing, the way I did is just wrote an activity which is not calling any view and defined the theme as android:theme="#android:style/Theme.NoDisplay" in manifest so the user won't notice after install that the app has already started and the receiver is also available. Thanks #Nitin Sethi for pointing me in the right direction.
public class MyActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
}
Here is how the new manifest for AppReceiver.apk looks
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.appreceiver.MyActivity" android:theme="#android:style/Theme.NoDisplay">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.appreceiver.MyReceiver">
<intent-filter>
<action android:name="com.appreceiver.CUSTOM_INTENT"></action>
</intent-filter>
</receiver>
</application>
Related
As i am new to android programming, I don't know how to call a certain class and/or certain activity first.
For example i have two classes viz. 1) Login.java 2) Create.java and two xml files associated with them are activity_main.xml and create_new.xml respectively.
So how can i make Login.java run first with activity_main.xml as screen?
in login java use this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);}
in create java us this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_new);
in androidmanifest
<activity
android:name=".Login"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
and another activities
<activity android:name=".MainActivity" ></activity>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.your.package"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.your.package.activity.Login"
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="com.example.your.package.activity.Create"
android:label="#string/app_name" >
</activity>
</application>
</manifest>
In this case, the intent-filter of LOGIN activity specifies that the intent with action of MAIN and category of LAUNCHER will be caught by it, aka it is where the application starts.
Afterwards,
public class Login extends Activity
{
//honestly I'd name this class LoginActivity and same in the XML
#Override
public void onCreate(Bundle saveInstanceState)
{
super.onCreate(saveInstanceState);
setContentView(R.layout.activity_main);
}
....
}
Also look at this example to learn how to use Fragments:
NullPointerException accessing views in onCreate()
from the intent filters you decalre in the android manifest file of your project;see the image
You declare an intent filter in the activity tag something like shown in the image for making it the first activity of your application.and if there are other activities in your app having intent filters than you have to just change the capital letter MAIN to DEFAULT in the intent filter tag
You will find AndroidManifest file like in the following image in your project
I see this has been asked quite a bit, but I can't seem to resolve my problem with what is out there.
My onReceive() method in broadcast receiver isn't being called.
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.app.test.TestActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:theme="#style/FullscreenTheme" >
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<receiver android:enabled="true" android:name=".BootUpReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
</manifest>
BootUpReceiver.java
package com.app.test;
public class BootUpReceiver extends BroadcastReceiver {
private static final String TAG = "TESTAPP_BootUpReceiver";
#Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "helllllllllllllllo");
Toast.makeText(context, "boot completed received", Toast.LENGTH_LONG).show();
// Intent i = new Intent(context, TestActivity.class);
// i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// context.startActivity(i);
}
}
Have tried using the entire path instead of .BootUpReceiver, didn't work. Not seeing anything from logcat or any Toast messages. Going into adb shell and emitting the boot_completed event that way doesn't help as the device then reboots.
Is there anything I am doing wrong? I read something about applications being inactive when device boots, does that affect my problem?
Here are some reference in Android developer website
http://developer.android.com/guide/topics/data/install-location.html
Broadcast Receivers listening for "boot completed"
The system delivers the ACTION_BOOT_COMPLETED broadcast before the external storage is mounted to the device. If your application is installed on the external storage, it can never receive this broadcast.
I am building an android app which once installed helps automating some of my home controls. As the first simple feature of the app, i want the phone to toast me with welcome message whenever i enter my home.
I thought i could implement this by putting in a BroadCastReceiver with action name "android.net.wifi.WIFI_STATE_CHANGED" and i assumed, whenever a new network is available, the broadcast receiver will be notified and i could check the SSID to see if i have arrived home.
Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.myfirstapp.MainActivity"
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="com.example.myfirstapp.WifiNetworksAvailableBroadcastReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
</intent-filter>
</receiver>
<activity
android:name="com.example.myfirstapp.WifiTester"
android:label="#string/title_activity_wifi_tester" >
</activity>
</application>
</manifest>
My WifiNetworksAvailableBroadcastReceiver looks like:
public class WifiNetworksAvailableBroadcastReceiver extends BroadcastReceiver {
protected static final String TAG = "MyApp";
#Override
public void onReceive(Context context, Intent intent) {
WifiManager mMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
Log.i(TAG, "------");
for(final ScanResult result:mMgr.getScanResults()) {
if(result.SSID.equals("<MY_HOME_WIFI_SSID>")) {
Toast.makeText(context, "Welcome home", Toast.LENGTH_LONG).show();
}
}
Log.i(TAG, "======");
}
}
Now with the app installed. I enter my home but i do not get the toast. I tried running the app in the background but still i did not get the toast. Could anyone give an direction? Is my approach correct?
Try to change the receiver with the event SCAN_RESULTS_AVAILABLE_ACTION.
Your receiver will be called every time a new wifi scan has been done, then you can check if your SSID is present in the list of available ones calling getScanResults() and eventually show the toast.
PS.
Have you registered your receiver?
I am trying to write an app where you can type in an address and then you get redirected to google maps. (I suppose this is called implicit intent)
-I have created an intent to launch the main activity, which is the only activity in my app.
The Main activity consists of some text, an editfield and a button.
AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.where_do_you_live"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
this is the code for the button:
public void Button1Click(View view)
{
try
{
addressField=(EditText)findViewById(R.id.address);
String address=addressField.getText().toString();
address=address.replace(' ','+');
Intent geoIntent=new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("geo:0,0?q=" + address));
startActivity(geoIntent);
}
catch(Exception e)
{
TextView tv=(TextView)findViewById(R.id.textView1);
tv.setText(e.toString());
//finding stuff
}
}
If you are testing this in emulator, things are different than in a device.
When you are creating your Android Virtual Device, you should select Google APIs as your target. If you do not have them installed, you can use SDK Manager to download it.
Have a look at this.
So I am trying to develop a custom lockscreen
but my broadcastreceiver won't fire
my manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.alexander.fuchs.lockscreen"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".app"
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=".myreceiver">
<intent-filter>
<action android:name="android.intent.action.SCREEN_OFF"/>
<action android:name="android.intent.action.SCREEN_ON"/>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
</manifest>
my receiver :
public class myreceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("receiver","works");
Toast.makeText(context,"works",Toast.LENGTH_LONG).show();
Intent in=new Intent(context,app.class);
context.startActivity(in);
}
}
the receiver should show me that it is fired :D
but ther aren't any logs in logcat
well , for the screen off and screen on , this cannot be inside the manifest , but only at runtime . see this:
http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/
for the bootup , it must be in the manifest , so something else is wrong with it.check the path of the class. this is surely the cause of the problem.