I am trying to use broadcast receivers to check if there is internet or not , I dont get an error but my application is not working , it doesnt inform about the existence of the internet .. Could anybody help ? here are my codes ... Thanks in advance
package com.example.internetconnection;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import android.support.v4.app.NavUtils;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BroadcastReceiver networkStateReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE );
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE );
if ( activeNetInfo != null )
{
Toast.makeText( context, "Active Network Type : "+ activeNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();
}
if( mobNetInfo != null )
{
Toast.makeText( context, "Mobile Network Type : "+ mobNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();
}
}
};
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(networkStateReceiver, filter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Androidmanifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.internetconnection"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<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>
<receiver android:name="com.example.internetconnection"
android:label="NetworkConnection">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
</application>
activity_main.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.internetconnection"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<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>
<receiver android:name="com.example.internetconnection"
android:label="NetworkConnection">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
</application>
You have registered it dynamically , then why you are using receiver tag in manifest. I think you are messing up the code .
Related
I am currently working on call announcer for android
My code works perfect on some devices but it do not work on some devices
Following is my code
public void onReceive(Context context, Intent intent) {
String state=intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if(state.equals(TelephonyManager.EXTRA_STATE_RINGING))
{
String number=intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Toast.makeText(context, "Incoming Number:"+number, Toast.LENGTH_SHORT).show();
}
}
the number is toasted on some devices but not all devices
Main Activity
package com.apcoms.smsandcallannouncer;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PhoneCallListener phoneListener = new PhoneCallListener();
}
}
Manifest File
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.apcoms.smsandcallannouncer">
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<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=".PhoneCallListener">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>
</application>
</manifest>
The following is the code for my Android splash screen activity.
When it attempts to load another activity, the application crashes on Android 4.0 and 4.1.
I have no idea what's causing this because when it crashes, it does not log any error.
Has anyone come across anything like this befere?
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import org.bitbucket.infovillafoundation.denko.R;
import org.bitbucket.infovillafoundation.denko.component.DaggerDenkoStationComponent;
import org.bitbucket.infovillafoundation.denko.component.DenkoStationComponent;
import org.bitbucket.infovillafoundation.denko.models.DenkoModel;
import org.bitbucket.infovillafoundation.denko.module.DenkoStationModule;
import org.bitbucket.infovillafoundation.denko.service.DenkoStationService;
import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;
public class SplashScreenActivity extends Activity {
#InjectView(R.id.imgLogo)
ImageView logoImage;
#InjectView(R.id.welcomeText)
TextView welcomeText;
private Handler splashHandler = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Runnable r = new Runnable() {
#Override
public void run() {
//********************* application is crashing here
Intent mainIntent = new Intent(SplashScreenActivity.this, LanguageOptionActivity.class);
startActivity(mainIntent);
finish();
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}
};
setContentView(R.layout.activity_splash);
ButterKnife.inject(this);
DenkoStationComponent component = DaggerDenkoStationComponent.builder().denkoStationModule(new DenkoStationModule()).build();
final DenkoStationService denkoStationService = component.provideDenkoStationService();
Callback<DenkoModel> callback = new Callback<DenkoModel>() {
#Override
public void success(DenkoModel denkoModel, Response response) {
toast(R.string.server_connection_successful);
denkoStationService.updateDatabaseWithDenkoModel(denkoModel);
}
#Override
public void failure(RetrofitError error) {
toast(R.string.server_connection_failed);
}
public void toast(int textId) {
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_layout,
(ViewGroup) findViewById(R.id.toast_layout_root));
TextView text = (TextView) layout.findViewById(R.id.toast_text);
text.setText(getResources().getString(textId));
Toast toast = new Toast(getApplicationContext());
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}
};
denkoStationService.fetchDenkoModel(denkoStationService.fetchDenkoLastDataState(), callback);
if (isNetworkAvailable())
splashHandler.postDelayed(r, 3000);
else {
toast(R.string.no_internet_connection);
splashHandler.postDelayed(r, 3000);
}
//********************* application is crashing here
Intent mainIntent = new Intent(this, LanguageOptionActivity.class);
startActivity(mainIntent);
}
#Override
protected void onResume() {
super.onResume();
}
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null;
}
private void toast(int textId) {
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_layout,
(ViewGroup) findViewById(R.id.toast_layout_root));
TextView text = (TextView) layout.findViewById(R.id.toast_text);
text.setText(getResources().getString(textId));
Toast toast = new Toast(getApplicationContext());
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}
}
Edit: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.bitbucket.infovillafoundation.denko">
<uses-sdk android:minSdkVersion="14" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<permission
android:name="org.bitbucket.infovillafoundation.denko.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="org.bitbucket.infovillafoundation.denko.MAPS_RECEIVE" />
<application
android:name=".application.DenkoApplication"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity
android:name=".activity.SplashScreenActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activity.MainActivity"
android:label="#string/title_activity_home"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.NoTitleBar" />
<activity
android:name=".activity.LanguageOptionActivity"
android:label="#string/title_activity_language_option"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.NoTitleBar" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyAubZsgoC4Z64qcOSpfK4grjrK5zrTEWxk" />
</application>
</manifest>
I think that since the crashes do not occur with a higher Android version, the problem might be resulting from an incompatible OpenGL version.
You can use following Activity with some simple content. Inject the layout (see below) and register in the layout file an onclick-listener (method "forward"). In this method you can call your next activity:
public class SplashScreen extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);
}
public void forward(View v){
startActivity(new Intent(this, MainActivity.class), 0);
}
}
splashscreen.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageView
android:id="#+id/splashscreen"
android:onClick="forward"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/ic_launcher" />
</LinearLayout>
Add in your AndroidManifest.xml:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" >
<activity
android:name=".SplashScreen"
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=".MainActivity" />
</application>
The class "MainActivity" represents your main activity and the class "SplashScreen" represents a simple splash screen.
I hope this will help you:)
The problem i see is that while calling the activity tag you still use .activity in the name attribute
i.e you used android:name=".activity.MainActivity"
instead of android:name=".MainActivity" both for splash screen activity and main activity.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.bitbucket.infovillafoundation.denko">
<uses-sdk android:minSdkVersion="14" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<permission
android:name="org.bitbucket.infovillafoundation.denko.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="org.bitbucket.infovillafoundation.denko.MAPS_RECEIVE" />
<application
android:name=".application.DenkoApplication"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity
android:name=".SplashScreenActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/title_activity_home"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.NoTitleBar" />
<activity
android:name=".activity.LanguageOptionActivity"
android:label="#string/title_activity_language_option"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.NoTitleBar" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyAubZsgoC4Z64qcOSpfK4grjrK5zrTEWxk" />
</application>
</manifest>
I think you cannot added your splash screen in AndroidManifest.xml.plz check
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class Splash extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread logoTimer = new Thread() {
public void run() {
try {
int logoTimer = 0;
while (logoTimer < 500) {
sleep(100);
logoTimer = logoTimer + 100;
}
Intent newIntent = new Intent(Splash.this, MainActivity.class);
startActivity(newIntent);
} catch(InterruptedException e) {
e.printStackTrace();
} finally {
finish();
}
}
};
logoTimer.start();
}
}
in Android Manifest.xml
<activity
android:name=".Splash"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I am testing the following app on HTC with Android 2.3.5. For some reason app is not launching on restarting or booting the phone.
I need to know where am I wrong exactly?
BootReciever.java
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.widget.Toast;
public class BootupReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "App started", Toast.LENGTH_LONG).show();
/*
Intent startActivityIntent = new Intent(context, MainActivity.class);
startActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startActivityIntent);
*/
// context.startActivity(new Intent(context, MainActivity.class));
}
}
MainActivity.java
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.content.BroadcastReceiver;
import android.widget.Toast;
import android.content.Intent;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Android Manifest File:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidbootreciever"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.hello_android_world.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<receiver android:name="com.example.androidbootreciever.BootupReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
</manifest>
The code is working on restart but not on turning on the phone after shutting it down. Here are the messages that I receive in my logcat:
You extended BootupReceiver with Activity?
It should be extended with Broadcast Receiver.
public class BootupReceiver extends BroadcastReceiver{
}
Your Receiver:
public class AutoStartBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent receivedIntent) {
context.startActivity(new Intent(context, YourActivity.class));
}
}
Your Manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name="com.example.receiver.AutoStartBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Good luck!
You need to write following permission,
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
If your application is stored on External storage then you should provide following intent filter,
<receiver android:name=".BootupReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
See App Install Location.
I try to write app which can start when usb connect
I learn from Starting my android application automatically after connecting the USB cable.
my code is
package com.example.formatsdcard;
import java.io.File;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MainActivity extends Activity {
public class OnPowerReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button mButton01 = (Button)findViewById(R.id.button1);
mButton01.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
wipeMemoryCard();
}
});
MY Manifest is
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.formatsdcard"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.formatsdcard.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=".OnPowerReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
</intent-filter>
</receiver>
</application>
my app can run after I click it but it will show there're some problem try again later when I connect my usb cable.
How should I change my code to let it work normally.
You should probably try
<receiver android:name="MainActivity$OnPowerReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
</intent-filter>
</receiver>
Or try putting OnPowerReceiver as a public class in your package com.example.formatsdcard
Create a own class for your onPowerReceiver. Don't put it in your MainActivity.
I am trying to generate some log and put up a toast on screen after power button click.
But it doesn't seem to work. Here's the code:
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.pbtest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACTION_SHUTDOWN" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.test.pbtest.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.test.pbtest.MyReceiver">
<intent-filter>
<action android:name="android.intent.action.SCREEN_OFF"></action>
<action android:name="android.intent.action.SCREEN_ON"></action>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"></action>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"></action>
<action android:name="android.intent.action.ACTION_SHUTDOWN"></action>
</intent-filter>
</receiver>
</application>
</manifest>
MyReceiver.java class:
package com.test.pbtest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
Log.v("Hurray!", "Power button was clicked!");
Toast.makeText(arg0, "power button clicked",Toast.LENGTH_LONG).show();
}
}
MainActivity.java class:
package com.test.pbtest;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
As per my knowledge, i need not register my receiver explicitly as i have used it in manifest file. Please guide me where i am going wrong.
you will need to add ACTION_SHUTDOWN permission in AndroidManifest.xml :
<uses-permission android:name="android.permission.ACTION_SHUTDOWN" />
for some reason you have to register your receiver on runtime.. you can register it via service