I am trying to launch and activity after boot.But app is getting crashed.
Its giving error
java.lang.RuntimeException: Unable to start receiver com.example.shoky.onboot.OnBoot: android.content.ActivityNotFoundException: Unable to find explicit activity class
OnBoot.java
package com.example.shoky.onboot;
import android.util.Log;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
/**
* Created by Shoky on 5/22/2016.
*/
public class OnBoot extends BroadcastReceiver {
#Override
public void onReceive(Context context,Intent intent){
Log.w("ASHOK","ONBOOT");
Intent myIntent = new Intent(context,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.shoky.onboot" >
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver android:name=".OnBoot"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name= "android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<activity
android:name=".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>
MainActivity.java
package com.example.shoky.onboot;
import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
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.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Try with changing this following in your manifest
<receiver
android:name=".OnBoot"
android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Do something like this in your OnBoot class
public void onReceive(Context context,Intent intent){
Log.w("ASHOK","ONBOOT");
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Intent myIntent = new Intent(context,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
Add android:permission="android.permission.RECEIVE_BOOT_COMPLETED"
inside receiver.
Include "category android:name="android.intent.category.DEFAULT"
inside intent filter.
<intent-filter>
<action android:name= "android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
Inside onReceive method,
Intent reference(myIntent) you have created is different with what you are calling at startActivity(intent).
Note: intent provided by onReceive method is used to filter the action event with which it has been registered.
Eg:
if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
{ `enter code here`
//code
}
Related
I have a problem with running the service. In the logs there is nothing to see that the service is running. So I don't know that she works. Toast also does not show in MainActivity. I read a lot of posts and none work. How to fix it?
Service
import android.app.Service;
import android.os.IBinder;
import android.widget.Toast;
import android.content.Intent;
public class AutoStartUp 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
}
}
BroadcastReceiver
import android.content.Context;
import android.content.BroadcastReceiver;
import android.content.Intent;
public class BootComplete extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
Intent serviceIntent = new Intent(context, AutoStartUp.class);
context.startService(serviceIntent);
}
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="kamiszczu.ovh.servicetest3">
<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="kamiszczu.ovh.servicetest3.BootComplete"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service
android:name="kamiszczu.ovh.servicetest3.AutoStartUp"
android:enabled="true">
</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>
</manifest>
You don't have to verify the intent type since the
Receiver is registered with only one intent type(Intent.ACTION_BOOT_COMPLETED). So there is no need to check in the receiver if the intent action is Intent.ACTION_BOOT_COMPLETED.
I think the condition in the receiver is not true and because of that the code that starts your service is not executed.
I want my application to launch browser when boot my tablet but its crashing on boot time. If someone please let me know my mistake? I write nothing in my activity_main.xmle. Its showing me message "unfortunately the application crashed".
mainfest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.autostartmyapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".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=".BootReciever" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
mainActivitc class
package com.example.autostartmyapp;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://google.com/"));
startActivity(browserIntent);
}
public class BootReciever extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(context, MainActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
}
}
separate both classes and fix mainfest like below
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.autostartmyapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".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.autostartmyapp.BootReciever"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
</manifest>
write MainActiity
MainActivity {
...}
BootReceiver class
package com.example.BootReciever;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class BootReciever extends BroadcastReceiver {
#Override
public void onReceive(Context ctx, Intent arg1) {
Intent iMainActivity = new Intent(ctx, yourMainActivityClass);
iMainStartActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(iMainActivity);
}
}
At a guess, I'd say it's probably because your manifest specifies .BootReceiver as the receiver, but based on your code the BootReceiver class is nested inside MainActivity. So you either need to move BootReceiver to a top-level class, or change your manifest to specify .MainActivity.BootReceiver as the receiver.
If that doesn't resolve it, please post your logcat so we can see what error is causing the crash.
Use this in the Manifest as receiver section:
<receiver
android:name=".BootReciever"
android:enabled="true"
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>
And take care of Broadcast Receiver class that should be not inner:
package com.example.BootReciever;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class BootReciever extends BroadcastReceiver {
#Override
public void onReceive(Context ctx, Intent arg1) {
Intent iMainActivity = new Intent(ctx, MainActivity.class);
iMainStartActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(iMainActivity);
}
}
Your app starts so quick and doesn't let other important apps to start first I think you should add some delay in your app to start up. like this:
public class BootUpReceiver extends BroadcastReceiver {
#Override
public void onReceive(final Context context, Intent intent) {
// We can't wait on the main thread as it would be blocked if we wait for too long
new Thread(new Runnable() {
#Override
public void run() {
try {
// Lets wait some time before starting the service to be fair to other processes on startup
Thread.sleep(5000);
} catch (InterruptedException e) {
}
Intent myIntent = new Intent(context, MainActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
}).start();
}
I am new here,posting my first question.So this android project about showing a layout onclick of button,isn't running.When I run it,the emulator opens but my project isn't visible on it. I am trying to show another layout when button is clicked.Can anyone help me out?
My layouts are :activity_main,tutorial1 (layout to be showed after button:tutorial1 is clicked),splash(which is showed for 5 seconds on opening).
Also manja is name of sound that is played on opening,for 5 seconds.
Here's my code:
MainActivity.java:
package com.example.thebasics;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
public class MainActivity extends Activity {
MediaPlayer logoMusic;
#Override
protected void onCreate(Bundle AmanIsAwesome) {
super.onCreate(AmanIsAwesome);
setContentView(R.layout.splash);
logoMusic = MediaPlayer.create(MainActivity.this, R.raw.manja);
logoMusic.start();
Thread logoTimer= new Thread (){
public void run (){
try{
sleep(5000);
Intent menuIntent=new Intent("com.example.thebasics.MENU");
startActivity(menuIntent);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
finish();
}
}
};
logoTimer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
logoMusic.release();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Menu.java:
package com.example.thebasics;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class menu extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button tut1 = (Button) findViewById(R.id.tutorial1);
tut1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent ("com.example.thebasics.TUTORIAL"));
}
});
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
Tutorial.java:
package com.example.thebasics;
import android.app.Activity;
import android.os.Bundle;
public class Tutorial extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tutorial1);
}
}
}
The basics androidmanifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.thebasics"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/download"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.thebasics.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAINACTIVITY" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.thebasics.MENU"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.thebasics.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.thebasics.TUTORIAL"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.thebasics.TUTORIAL" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Also the console is showing the error:- Emulator[ Warning: No DNS servers found]
whats this?
I have uploaded a fully working of your project.Download it from here.Also use Intent as other people mentioned.
Intent intent = new Intent(MainActivity.this or getApplicationContext(), Tutorial.class);
/* First argument is your current activity.You can also mention it i.e CurrentActivity.this */
/* Second argument is the class where you wanna go i.e OtherActivity.class */
startActivity(intent);
Use this instead :
startActivity(new Intent (context, Tutorial.class));
Your way of doing it is strange too :
dont do this :
<activity
android:name="com.example.thebasics.TUTORIAL"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.thebasics.TUTORIAL" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Just do :
<activity
android:name="com.example.thebasics.Tutorial"
android:label="#string/app_name" >
</activity>
Same thing for the other one.
This doesn't exist :
<action android:name="com.example.thebasics.TUTORIAL" />
This line code
new Intent ("com.example.thebasics.TUTORIAL")
Creates intent with action name "com.example.thebasics.TUTORIAL"
What you want is:
startActivity(new Intent (MainActivity.this, Tutorial.class));
Just like Tsunaze suggested.
do as follow
Intent intent = new Intent(context, Tutorial.class);
// set some flags here according to your need
startActivity(intent);
Intent intent = new Intent(context, Menu.class);
// set some flags here according to your need
startActivity(intent);
and remove this from your manifest file as this is wrong
<intent-filter>
<action android:name="com.example.thebasics.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="com.example.thebasics.TUTORIAL" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
I am trying to get the registration id of a device (testing using the emulator) to set up push notifications using GCM.
I have tried the following code, which gives me the following error:
Unable to start service Intent { act=com.google.android.c2dm.intent.REGISTRATION (has extras) } U=0: not found
Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.me.pushnotifications"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<permission android:name="com.me.pushnotifications.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.me.pushnotifications.permission.C2D_MESSAGE" />
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver android:name="com.me.pushnotifications.MyBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE"/>
<action android:name="com.google.android.c2dm.intent.REGISTRATION"/>
<category android:name="com.me.pushnotifications"/>
</intent-filter>
</receiver>
<activity
android:name="com.me.pushnotifications.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>
I am using a BroadcastReceiver to achieve the purpose:
package com.me.pushnotifications;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyBroadcastReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context arg0, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
Log.i("action::",action);
try{
if(action.equals("com.google.android.c2dm.intent.REGISTRATION")){
String regId = intent.getStringExtra("registration_id");
String error = intent.getStringExtra("error");
String unregistered = intent.getStringExtra("unregistered");
Log.i("regId::",regId);
}
else if(action.equals("com.google.android.c2dm.intent.RECEIVE")){
String data = intent.getStringExtra("data");
Log.i("data::",data);
}
}
finally{
}
}
}
MainActivity
package com.me.pushnotifications;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Context context = getApplicationContext();
final String appId = getResources().getString(R.string.appId) ;
Log.i("appName::",appId);
Button activateButton,deactivateButton;
activateButton = (Button)findViewById(R.id.activateButton);
deactivateButton = (Button)findViewById(R.id.deactivateButton);
activateButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("activate::","clicked");
Intent regIntent = new Intent("com.google.android.c2dm.intent.REGISTRATION");
Log.i("intent::","created");
regIntent.putExtra("app", PendingIntent.getBroadcast(context, 0,
new Intent(), 0));
regIntent.putExtra("sender", "registration_id");
startService(regIntent);
}
});
deactivateButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("deactivate::","clicked");
Intent unregIntent = new Intent("com.google.android.c2dm.intent.UNREGISTER");
Log.i("intent::","created");
unregIntent.putExtra("app", PendingIntent.getBroadcast(context, 0,
new Intent(), 0));
startService(unregIntent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
I have installed the Google API's for API Level 18. What may be the reason for the issue?
add this line in your intent-filter tag in manifest
file
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
Like this
<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.androidhive.pushnotifications" />
</intent-filter>
Use like this:
<receiver
android:name="com.example.gcmclient.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<!-- Receives the registration id. -->
<action android:name="com.google.android.c2dm.intent.REGISTER" />
<category android:name="com.example.gcmclient" />
</intent-filter>
</receiver>
<service android:name="com.example.gcmclient.GcmMessageHandler" />
<meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<service android:name=".GcmIntentService" />
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