Need Context when no activity is running

前端 未结 5 461
离开以前
离开以前 2021-01-26 08:16

I am displaying a pop up from Notification of my application.But when I check my notification no activity is running from my application so in notification when I show dialogue

相关标签:
5条回答
  • 2021-01-26 08:55

    You may create MyApplication class, inherited from the Application, then inside that class do the following:

    public class MyApplication extends Application {
        private static MyApplication instance;
    
        @Override
        public void onCreate() {
            super.onCreate();
            instance = this;
            .........
        }
    
        public static Context getContext() {
            return instance;
        }
    

    After that, you may use MyApplication.getContext() anywhere if you need a context and don't have an Activity lying around.

    0 讨论(0)
  • 2021-01-26 08:59
    @Override
        protected Dialog onCreateDialog(int i) 
        {
                   LayoutInflater factory = LayoutInflater.from(this);
                   final View textEntryView = factory.inflate(R.layout.sendemail, null);
                   final View textEntryView1 = factory.inflate(R.layout.sendemail_title, null);
                  to_id = (EditText) textEntryView.findViewById(R.id.edit_text_sender_mail_id);
                  from_id =(EditText) textEntryView.findViewById(R.id.id);
                  pwd =(EditText) textEntryView.findViewById(R.id.pwd);
    
    
                   return new AlertDialog.Builder(current_activity.this)
                       .setTitle("hello")
                       .setCustomTitle(textEntryView1)
                       .setView(textEntryView)
                       .setPositiveButton("Send", new DialogInterface.OnClickListener() 
                       {
                           public void onClick(DialogInterface dialog, int whichButton)
                           {
    
                        // code here...    
                           }
                       })
                       .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int whichButton)
                           {
                                System.exit(0);
                           }
                       })
                       .create();
    
    
        }
    
    0 讨论(0)
  • 2021-01-26 09:05

    I suffered with the same issue. I can't able to use the spinner in my activity. I solved this by using the setContentView like as following

    public void onCreate(Bundle savedInstanceState) 
    {   
     super.onCreate(savedInstanceState);
     View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.main, null);
            this.setContentView(viewToLoad);
    }
    

    Another way you can use the separate activity for you dialog. By setting activity like follows

    style.xml
    
    <resources>
    
        <style name="AppTheme" parent="android:Theme.Dialog">
            <item name="android:windowNoTitle">true</item>
        </style>
    
    </resources>
    

    In AndroidManifest Declare activity like this

    <activity
    android:name="DialogActivity"
    android:theme="@style/AppTheme" />
    
    0 讨论(0)
  • I would assume that your code is running in a background in an Android Service. If that's the case, your Service is a Context itself, so you can use it when you need one.

    Of your code is running in a background thread and not in an Android Service (in which case you have other big problems), you could use an instance of the application context. You get one though calling Context.getApplicationContext, and you can get it in the Activity that starts your background code and cache it for later.

    Note though that there are certain gotchas with using application context, instead of activity context - for example, you can't use LayoutInflater from application context.

    0 讨论(0)
  • 2021-01-26 09:17

    you can make your dialog extend and Activity class and change its theme to dialog in the manifest file. in this case, you call notification.setLatestEventInfo(YourActivityDialog.this, contentTitle, contentText, contentIntent);.

    0 讨论(0)
提交回复
热议问题