在我看过的各种Android代码中:
public class MyActivity extends Activity {
public void method() {
mContext = this; // since Activity extends Context
mContext = getApplicationContext();
mContext = getBaseContext();
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我找不到任何合适的解释,哪些更好,以及在什么情况下应该使用.
关于这方面的文件的指示,以及关于如果选择了错误的可能会破坏的指导,将不胜感激.
在我的中Service,我使用以下代码在正常运行时打开通知:
private final static NOTIFICATION_ID = 412434;
private void startNotification() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(
this);
builder.setSmallIcon(R.drawable.notification);
builder.setContentTitle("Running");
final Intent intent = new Intent(this, MainActivity.class);
intent.setAction(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
final PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
builder.setOngoing(true);
builder.setAutoCancel(false);
notification = builder.build();
startForeground(NOTIFICATION_ID, notification);
}
Run Code Online (Sandbox Code Playgroud)
该PendingIntent是打开MainActivity时,通知被窃听.这在我的所有测试设备上运行得非常好,使用Android 2.3.3,2.3.5和Android 4.1.
它不起作用,但是在我的Nexus 7(Android 4.3)上,这根本不起作用.点击通知时没有任何反应.
我错过了这些放在一起的方式有什么变化吗?
我已经创建了一个管理短信的应用程序,我已经创建了通知,但是当我点击它们时它启动了另一个活动,我想知道如何检查一个活动是否已经停止并恢复它.
以下是用于创建pendingintent的代码:
private void createNotification(SmsMessage sms, Context context){
final NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
String contentTitle = "";
// construct the Notification object.
final NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentTitle(contentTitle)
.setContentText(sms.getMessageBody())
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(getIconBitmap())
.setNumber(nmessages);
builder.setAutoCancel(true);
//(R.drawable.stat_sample, tickerText,
// System.currentTimeMillis());
// Set the info for the views that show in the notification panel.
//notif.setLatestEventInfo(this, from, message, contentIntent);
/*
// On tablets, the ticker shows the sender, the first line of the message,
// the photo of the person and the app icon. …Run Code Online (Sandbox Code Playgroud)