android.app.ActivityThread.getApplicationThread()' on a null object reference - java

The microhonePopUp method will work in MainActivity, but I'd like for it to work from another class (MediaButtonIntentReceiver). The problem is with startActivityForResult(intent, REQUEST_CODE); - but I don't know how to resolve it.
in the MainActivity class
public void microphonePopUp(){
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Voice your answer");
startActivityForResult(intent, REQUEST_CODE);
}
in the MediaButtonIntentReceiver class
public class MediaButtonIntentReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (KeyEvent.KEYCODE_HEADSETHOOK == event.getKeyCode()) {
MainActivity test = new MainActivity();
test.microphonePopUp();
}
}
}
}
12-09 11:20:14.803 19556-19556/com.timtennyson.priceaddition
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.timtennyson.priceaddition, PID: 19556
java.lang.RuntimeException: Unable to start receiver
com.timtennyson.priceaddition.MediaButtonIntentReceiver:
java.lang.NullPointerException: Attempt to invoke virtual method
'android.app.ActivityThread$ApplicationThread
android.app.ActivityThread.getApplicationThread()' on a null object
reference
at android.app.ActivityThread.handleReceiver(ActivityThread.java:3641)
at android.app.ActivityThread.access$2000(ActivityThread.java:221)
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1876)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual
method 'android.app.ActivityThread$ApplicationThread
android.app.ActivityThread.getApplicationThread()' on a null object
reference
at android.app.Activity.startActivityForResult(Activity.java:4283)
at android.app.Activity.startActivityForResult(Activity.java:4230)
at
android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:842)
at
com.timtennyson.priceaddition.MainActivity.microphonePopUp(MainActivity.java:103)
at
com.timtennyson.priceaddition.MediaButtonIntentReceiver.onReceive(MediaButtonIntentReceiver.java:27)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:3634)
at android.app.ActivityThread.access$2000(ActivityThread.java:221) 
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1876) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:158) 
at android.app.ActivityThread.main(ActivityThread.java:7224) 
at java.lang.reflect.Method.invoke(Native Method)

NEVER create an instance of an activity, service, or provider yourself.
If your objective is to listen for ACTION_MEDIA_BUTTON broadcasts while MainActivity is visible:
Move MediaButtonIntentReceiver to be a nested class inside of MainActivity
Get rid of test from onReceive(), and just call microphonePopUp() (which I assume is a method on MainActivity)
Register your MediaButtonIntentReceiver using registerReceiver() in onStart() of MainActivity, and use unregisterReceiver() in onStop()
If your objective is to listen ACTION_MEDIA_BUTTON broadcasts at other points in time — by registering your receiver in the manifest — while you can do that, you cannot use microphonePopUp() from that receiver.

Related

App crashes when I call a constructor in an Activity

I am new to android studio and I am trying to build a Notepad app.
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
String title = findViewById(R.id.textView3).toString();
String note_content = findViewById(R.id.textView).toString();
FileOutputStream outputStream;
try
{
outputStream = openFileOutput(title, Context.MODE_PRIVATE);
outputStream.write(note_content.getBytes());
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
MainActivity mainActivity = new MainActivity(title);
}
});
This is the button a user clicks to save the note. Once the code saves the note, it should send the Title to MainActivity so that it can be sent to Recycleview Adapter - this will display it in viewholder as a text.
Presently, when I run the code, it crashes - however, when I remove the constructor, the app works fine.
( MainActivity mainActivity = new MainActivity(title);)
Error:
10-02 02:39:13.822 27279-27279/? D/AndroidRuntime: Shutting down VM
10-02 02:39:13.824 27279-27279/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.quicknote, PID: 27279
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.quicknote/com.example.quicknote.MainActivity}: java.lang.InstantiationException: java.lang.Class<com.example.quicknote.MainActivity> has no zero argument constructor
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2337)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by: java.lang.InstantiationException: java.lang.Class<com.example.quicknote.MainActivity> has no zero argument constructor
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1090)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5443) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
10-02 02:39:20.081 27279-27279/com.example.quicknote I/Process: Sending signal. PID: 27279 SIG: 9
As per the error message:
java.lang.InstantiationException: java.lang.Class has no zero argument constructor
A zero argument constructor is required for the Android system to instantiate an Activity. You should never be manually calling an Activity constructor yourself since only the system can properly create an Activity.
The Parcelables and Bundles documentation details the correct way of sending information to an Activity using the extras Bundle.
Unable to instantiate activity. Because you can't start an activity like this. You have to use intent to start activity. To sent "title" use intent extra.
Intent intent=new Intent(CurrentActivity.this, NewActivty.this);
intent.putExtra("title", title);
startActivty(intent);
Is your Activity in your AndroidManifest.xml?
If it is, you have to retrieve the title from your extras from onCreate() of MainActivity instead from the constructor.
Something like that:
//that code instead of your MainActivity mainActivity = new MainActivity(title); line
Intent intent = new Intent(YourActualActivity.this, MainActivity.class);
intent.putExtra("title", title);
startActivity(intent);
//That code in the onCreate method of your MainActivity
Bundle extras = getIntent().getExtras();
if (extras != null) {
String title = extras.getString("title");
}

Setting TextView text after onActivityResult

I have an app with a login page.
First, I have my MainActivity where everything starts:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
// here i launch LoginActivity
startActivityForResult(new Intent(this, LoginActivity.class), REQUEST_CODE);
emailText = findViewById(R.id.nav_text_email);
usernameText = findViewById(R.id.nav_text_username);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == REQUEST_CODE){
if(resultCode != Activity.RESULT_OK) {
finish();
}
// if login succesful, i attach to variable instance of
// logged user class.
// that part works correct, in object vars exist
loggedUser = loggedUser.getInstance();
// i want to attach strings after succesful login
emailText.setText(loggedUser.getEmail());
usernameText.setText(loggedUser.getUsername());
}
}
When run the app with the code above, after succesful LoginActivity app crashes with this exception:
2019-01-26 20:46:48.015 13071-13071/com.example.admin.keystroke_dynamics E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.admin.keystroke_dynamics, PID: 13071
java.lang.RuntimeException: Unable to resume activity {com.example.admin.keystroke_dynamics/com.example.admin.keystroke_dynamics.Activities.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3586)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3626)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2876)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.admin.keystroke_dynamics.Activities.MainActivity.onResume(MainActivity.java:89)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1279)
at android.app.Activity.performResume(Activity.java:7022)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3561)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3626) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2876) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567) 
at android.os.Handler.dispatchMessage(Handler.java:105) 
at android.os.Looper.loop(Looper.java:156) 
at android.app.ActivityThread.main(ActivityThread.java:6523) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832) 
When I deleted the setText() methods from `onActivityResult(), I don't get that exception. The error appears when I click the Login button and the app crashes.
While debugging, compiler not even go in lines with setText()
My question is, how to set texts in textviews in the scenario that I coded above?
UPDATE
Even if i popualte strings in loggedUser in constructor as empty, move loggedUser = loggedUser.getInstance() and move setText() to onCreat, i still get that NPE.
EDIT:
I added onResume method:
#Override
protected void onResume()
{
if(isLogged) {
emailText.setText(loggedUser.getEmail());
usernameText.setText(loggedUser.getUsername());
}
super.onResume();
}
And edited onActivityResult():
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == REQUEST_CODE){
if(resultCode != Activity.RESULT_OK) {
finish();
}
isLogged = true; //var for if statement in onResume()
loggedUser = loggedUser.getInstance();
}
}
And i still get the same error on onResume() method when i setting up TextViews.
2019-01-26 21:21:57.752 18262-18262/com.example.admin.keystroke_dynamics E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.admin.keystroke_dynamics, PID: 18262
java.lang.RuntimeException: Unable to resume activity {com.example.admin.keystroke_dynamics/com.example.admin.keystroke_dynamics.Activities.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3586)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3626)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1618)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.admin.keystroke_dynamics.Activities.MainActivity.onResume(MainActivity.java:90)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1279)
at android.app.Activity.performResume(Activity.java:7022)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3561)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3626) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1618) 
at android.os.Handler.dispatchMessage(Handler.java:105) 
at android.os.Looper.loop(Looper.java:156) 
at android.app.ActivityThread.main(ActivityThread.java:6523)
A null view is usually a result of your layout not containing a view with the id you specified in findViewById(). Therefore findViewById() returns null and the variables are remaining null when onResume() is called. Double check your view ids in main_activity.xml.
It's also possible that the full content view is not available in onCreate() if you are using a more complicated layout. If you find that your views are null in onCreate(), try moving findViewById() to a later point in the lifecycle.
Try reordering the code like this
emailText = findViewById(R.id.nav_text_email);
usernameText = findViewById(R.id.nav_text_username);
startActivityForResult(new Intent(this, LoginActivity.class), REQUEST_CODE);
Try initializing the variables before accessing them.
public class MainActivity extends AppCompatActivity {
TextView emailText;
TextView usernameText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
// here i launch LoginActivity
startActivityForResult(new Intent(this, LoginActivity.class), REQUEST_CODE);
emailText = findViewById(R.id.nav_text_email);
usernameText = findViewById(R.id.nav_text_username);
}
Could fit in a comment
You need to access the NavigationView from the activity
Update
NavigationView navigationView = findViewById(R.id.nav_id)
View header = navigationView.getHeaderView(0)
TextView usernameText = header.findViewById(R.id.username_text_view_id);

app crash with android.content.Context.getPackageName()' on a null object reference

i need help.
I'm in the finish phase of development of my android application and now after a few month of develop the app magically crash with this error :
java.lang.String android.content.Context.getPackageName()' on a null object reference
I don't know what is the problem but i will post the code that cause the crash :
public static void start(Context context) {
context.startActivity(new Intent(context, ConversationsActivity.class));
}
called with this piece of code (inside a fragment):
ConversationsActivity.start(getActivity());
The last change that i've make was to add Fabric.io ( in particular Branch ) and from the history in the last commit on git i don't show nothing that can produce this error
EDIT :
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.content.ComponentName.<init>(ComponentName.java:128)
at android.content.Intent.<init>(Intent.java:4900)
at com.exampleapp.views.messages.ConversationsActivity.start(ConversationsActivity.java:31)
at com.exampleapp.views.menu.MenuFragment.onMenuMessagesClicked(MenuFragment.java:116)
at com.exampleapp.views.menu.MenuFragment_ViewBinding$5.doClick(MenuFragment_ViewBinding.java:82)
at butterknife.internal.DebouncingOnClickListener.onClick(DebouncingOnClickListener.java:22)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Or you could do this:
public static void start(Activity activity) {
activity.startActivity(new Intent(activity, ConversationsActivity.class));
}
You should use getActivity() to launch an Activity from Fragment.
From a Fragment: Context is parent activity (getActivity()).
Intent intent = new Intent(getActivity(), ConversationsActivity.class);
startActivity(intent);
From an Activity: Context is current activity (this).
Intent intent = new Intent(this, ConversationsActivity.class);
startActivity(intent);
Your problem is that you are using the intent wrong. Replace your start() code with this:
public static void start() {
Intent i = new Intent(getApplicationContext(), ConversationsActivity.class);
startActivity(i);
}
Then instead of using ConversationsActivity.start(getActivity()); to call it, just use start(); when you want to call the method.
Hope this helps!

Start another activity in another class

I have a three class: MainActivity, BackgroundActivity and roomActivity.
BackgroundActivity will receipt the php server feedback.
If it is not login fail, it will call RoomActivity class.
I want to know that why is not functioning?
BackgroundActivity:
protected void onPostExecute(String result) {
if (result.equals("<meta charset=\"utf-8\">login fail")) {
alertDialog.setMessage("Please check your login email");
alertDialog.show();
} else {
**Intent myIntent = new Intent(MainActivity.class, RoomActivity.class);
startActivity(myIntent);**
}
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.a20_1discussboard, PID: 2262
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.content.ContextWrapper.getPackageName(ContextWrapper.java:132)
at android.content.ComponentName.<init>(ComponentName.java:77)
at android.content.Intent.<init>(Intent.java:4160)
at com.example.a20_1discussboard.MainActivity.check(MainActivity.java:38)
at com.example.a20_1discussboard.BackgroundWorker.onPostExecute(BackgroundWorker.java:151)
at com.example.a20_1discussboard.BackgroundWorker.onPostExecute(BackgroundWorker.java:24)
at android.os.AsyncTask.finish(AsyncTask.java:636)
at android.os.AsyncTask.access$500(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
change it to
Intent myIntent = new Intent(MainActivity.this, RoomActivity.class);
Calling startActivity() from outside of an Activity may produce AndroidRuntimeException, Use Context like:
Intent myIntent = new Intent(MainActivity.class, RoomActivity.class);
getApplicationContext().startActivity(myIntent);
Intent constructor is wrong.
it should be like :
Intent myIntent = new Intent(getApplicationContext(), RoomActivity.class);
startActivity(myIntent);
could you post more info about the issue ?

Android Notification from non-activity class

I want to call the notify() method which is in MainActivity from another class. This is how I did it:
public class MyAlarmService extends Service{
#Override
public IBinder onBind(Intent arg0)
{
// TODO Auto-generated method stub
return null;
}
#Override
public void onCreate()
{
// TODO Auto-generated method stub
super.onCreate();
}
#SuppressWarnings("static-access")
#Override
public void onStart(Intent intent, int startId)
{
super.onStart(intent, startId);
String title = "title";
String message = "message";
new MainActivity().Notify(title, message);
...
MainActivity:
public class MainActivity extends Activity {
NotificationManager manager;
Notification myNotication;
#Override
protected void onCreate(Bundle savedInstanceState) {
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
...
public void Notify(String notificationTitle, String notificationMessage){
Intent intent = new Intent("com.xxxx.app.MainActivity");
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 1, intent, 0);
Notification.Builder builder = new Notification.Builder(MainActivity.this);
builder.setAutoCancel(false);
builder.setTicker("this is ticker text");
builder.setContentTitle("Notification");
builder.setContentText("Text");
builder.setSmallIcon(R.drawable.infoicon);
builder.setContentIntent(pendingIntent);
builder.setOngoing(true);
builder.setSubText("This is subtext...");
builder.setNumber(100);
builder.build();
myNotication = builder.getNotification();
manager.notify(11, myNotication);
}
...
But I get an error message:
FATAL EXCEPTION: main
Process: com.xxxx.app, PID: 10311
java.lang.RuntimeException: Unable to start service com.xxxx.app.MyAlarmService#636e513 with Intent { cmp=com.xxxx.app/.MyAlarmService }: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3027)
at android.app.ActivityThread.-wrap17(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1442)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.content.ContextWrapper.getPackageName(ContextWrapper.java:133)
at android.app.PendingIntent.getActivity(PendingIntent.java:305)
at android.app.PendingIntent.getActivity(PendingIntent.java:272)
at com.xxxx.app.MainActivity.Notify(MainActivity.java:304)
at com.xxxx.app.MyAlarmService.onStart(MyAlarmService.java:41)
at android.app.Service.onStartCommand(Service.java:459)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3010)
at android.app.ActivityThread.-wrap17(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1442) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
You can do the same in following ways:
1) Copy that Notify method into the service and call it wherever you need order to trigger notification, Notification Service needs Context either Activity or service)
2) You can communicate to the activity from the service through broadcast receivers
3) You can do by using aidl
Trigger the notification in the Service class inside the onStart() method, and start the service from the MainActivity class using an Intent. Also, don't forget to declare your service in the manifest.
Here's the code to start the service from activity:
Intent intent = new Intent(this, MyAlarmService.class);
startService(intent);

Categories